-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmain.js
More file actions
209 lines (179 loc) · 7.47 KB
/
main.js
File metadata and controls
209 lines (179 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* main.js
* http://www.codrops.com
*
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright 2015, Codrops
* http://www.codrops.com
*/
(function() {
//The Document.documentElement read-only property returns the Element that is the root element of the document (for example, the <html> element for HTML documents).
var docElem = window.document.documentElement,
// transition end event name
//The transitionend event is fired when a CSS transition has completed. In the case where a transition is removed before completion, such as if the transition-property is removed, then the event will not fire.
transEndEventNames = { 'WebkitTransition': 'webkitTransitionEnd', 'MozTransition': 'transitionend', 'OTransition': 'oTransitionEnd', 'msTransition': 'MSTransitionEnd', 'transition': 'transitionend' },
transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ];
function scrollX() { return window.pageXOffset || docElem.scrollLeft; }
function scrollY() { return window.pageYOffset || docElem.scrollTop; }
function getOffset(el) {
var offset = el.getBoundingClientRect();
return { top : offset.top + scrollY(), left : offset.left + scrollX() };
}
function dragMoveListener(event) {
var target = event.target,
// keep the dragged position in the data-x/data-y attributes
x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
// translate the element
target.style.transform = target.style.webkitTransform = 'translate(' + x + 'px, ' + y + 'px)';
target.style.zIndex = 10000;
// update the posiion attributes
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
}
function revertDraggable(el) {
el.style.transform = el.style.webkitTransform = 'none';
el.style.zIndex = 1;
el.setAttribute('data-x', 0);
el.setAttribute('data-y', 0);
}
function init() {
document.querySelector('button.info-close').addEventListener('click', function() {
var info = document.querySelector('.info-wrap');
info.parentNode.removeChild(info);
});
// target elements with the "drag-element" class
interact('.drag-element').draggable({
// enable inertial throwing
inertia: true,
// call this function on every dragmove event
onmove: dragMoveListener,
onend: function (event) {
if(!classie.has(event.target, 'drag-element--dropped') && !classie.has(event.target, 'drag-element--dropped-text')) {
revertDraggable(event.target);
}
}
});
// enable draggables to be dropped into this
interact('.paint-area').dropzone({
// only accept elements matching this CSS selector
accept: '.drag-element',
// Require a 75% element overlap for a drop to be possible
overlap: 0.75,
ondragenter: function (event) {
classie.add(event.target, 'paint-area--highlight');
},
ondragleave: function (event) {
classie.remove(event.target, 'paint-area--highlight');
},
ondrop: function (event) {
var type = 'area';
if(classie.has(event.target, 'paint-area--text')) {
type = 'text';
}
if(classie.has(event.target, 'paint-area--svg')) {
type = 'svg';
}
var draggableElement = event.relatedTarget;
classie.add(draggableElement, type === 'text' ? 'drag-element--dropped-text' : 'drag-element--dropped');
var onEndTransCallbackFn = function(ev) {
this.removeEventListener( transEndEventName, onEndTransCallbackFn );
if( type === 'area' || type === 'svg') {
paintArea(event.dragEvent, event.target, draggableElement.getAttribute('data-color'));
}
setTimeout(function() {
revertDraggable(draggableElement);
classie.remove(draggableElement, type === 'text' ? 'drag-element--dropped-text' : 'drag-element--dropped');
}, type === 'text' ? 0 : 250);
};
if( type === 'text' ) {
paintArea(event.dragEvent, event.target, draggableElement.getAttribute('data-color'));
}
draggableElement.querySelector('.drop').addEventListener(transEndEventName, onEndTransCallbackFn);
},
ondropdeactivate: function (event) {
// remove active dropzone feedback
classie.remove(event.target, 'paint-area--highlight');
}
});
// reset colors
document.querySelector('button.reset-button').addEventListener('click', resetColors);
}
function paintArea(ev, el, color) {
var type = 'area';
if(classie.has(el, 'paint-area--text')) {
type = 'text';
}
if(classie.has(el, 'paint-area--svg')) {
type = 'svg';
}
console.log(type);
if( type === 'area' || type === 'svg' ) {
// create SVG element
var dummy = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
//setAttributeNS adds a new attribute or changes the value of an attribute with the given namespace and name.
dummy.setAttributeNS(null, 'version', '1.1');
dummy.setAttributeNS(null, 'width', '100%');
dummy.setAttributeNS(null, 'height', '100%');
dummy.setAttributeNS(null, 'class', 'paint');
var g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
g.setAttributeNS(null, 'transform', 'translate(' + Number(ev.pageX - getOffset(el).left) + ', ' + Number(ev.pageY - getOffset(el).top) + ')');
var circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
circle.setAttributeNS(null, 'cx', 0);
circle.setAttributeNS(null, 'cy', 0);
circle.setAttributeNS(null, 'r', Math.sqrt(Math.pow(el.offsetWidth,2) + Math.pow(el.offsetHeight,2)));
circle.setAttributeNS(null, 'fill', color);
dummy.appendChild(g);
g.appendChild(circle);
el.appendChild(dummy);
}
setTimeout(function() {
classie.add(el, 'paint--active');
if( type === 'text' ) {
el.style.color = color;
var onEndTransCallbackFn = function(ev) {
if( ev.target != this ) return;
this.removeEventListener( transEndEventName, onEndTransCallbackFn );
classie.remove(el, 'paint--active');
};
el.addEventListener(transEndEventName, onEndTransCallbackFn);
} else if( type === 'svg') {
var changeFill = function() {
// if( ev.target != this) return;
this.removeEventListener(transEndEventName, onEndTransCallbackFn);
// set the color
el.setAttribute("fill", color);
el.setAttribute("stroke", color);
// remove SVG element
el.removeChild(dummy);
setTimeout(function() { classie.remove(el, 'paint--active'); }, 25);
};
el.style.transition = el.style.MSTransitionEnd = el.style.OTransition= el.style.MozTransition = el.style.WebkitTransition = "none";
el.style.transition = el.style.MSTransitionEnd = el.style.OTransition= el.style.MozTransition = el.style.WebkitTransition = "fill 0.4s ease-in-out, stroke 0.4s ease-in-out";
setTimeout(function(){changeFill();}, 200);
}
else {
var onEndTransCallbackFn = function(ev) {
if( ev.target != this || ev.propertyName === 'fill-opacity' ) return;
this.removeEventListener(transEndEventName, onEndTransCallbackFn);
// set the color
el.style.backgroundColor = color;
// remove SVG element
el.removeChild(dummy);
setTimeout(function() { classie.remove(el, 'paint--active'); }, 25);
};
circle.addEventListener(transEndEventName, onEndTransCallbackFn);
}
}, 25);
}
function resetColors() {
[].slice.call(document.querySelectorAll('.paint-area')).forEach(function(el) {
el.style[classie.has(el, 'paint-area--text') ? 'color' : 'background-color'] = '';
el.setAttribute('fill', " #fafafa");
el.setAttribute('stroke', " #999999");
});
}
init();
})();