-
Notifications
You must be signed in to change notification settings - Fork 43
/
jCanvaScript.1.5.18.js
216 lines (209 loc) · 8.11 KB
/
jCanvaScript.1.5.18.js
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
210
211
212
213
214
215
216
/*!
* jCanvaScript JavaScript Library v 1.5.18
* http://jcscript.com/
*
* Copyright 2012, Alexander Savin
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function (window, undefined) {
var canvases = [],
m = Math,
m_pi = m.PI,
pi2 = m_pi * 2,
lastCanvas = 0, lastLayer = 0,
underMouse = false,
underMouseLayer = false,
regHasLetters = /[A-z]+?/,
regNumsWithMeasure = /\d.\w\w/,
FireFox = window.navigator.userAgent.match(/Firefox\/\w+\.\w+/i),
radian = 180 / m_pi,
m_max = m.max,
m_min = m.min,
m_cos = m.cos,
m_sin = m.sin,
m_floor = m.floor,
m_round = m.round,
m_abs = m.abs,
m_pow = m.pow,
m_sqrt = m.sqrt,
fps = 1000 / 60,
requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback, element) {
return setTimeout(callback, fps);
}
})(),
cancelRequestAnimFrame = (function () {
return window.cancelAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame ||
window.oCancelRequestAnimationFrame ||
window.msCancelRequestAnimationFrame ||
clearTimeout
})();
if (FireFox != "" && FireFox !== null)
var FireFox_lt_7 = (parseInt(FireFox[0].split(/[ \/\.]/i)[1]) < 7);
function findById(i, j, stroke) {
var objs = canvases[i].layers[j].objs,
grdntsnptrns = canvases[i].layers[j].grdntsnptrns,
objsLength = objs.length,
grdntsnptrnsLength = grdntsnptrns.length;
stroke = stroke.slice(1);
for (var k = 0; k < objsLength; k++)
if (objs[k].optns.id == stroke)return objs[k];
for (k = 0; k < grdntsnptrnsLength; k++)
if (grdntsnptrns[k].optns.id == stroke)return grdntsnptrns[k];
return false;
}
function findByName(i, j, myGroup, stroke) {
var objs = canvases[i].layers[j].objs,
grdntsnptrns = canvases[i].layers[j].grdntsnptrns,
strokes = stroke.slice(1).split('.');
findByNames(myGroup, strokes, objs);
findByNames(myGroup, strokes, grdntsnptrns);
return myGroup;
function findByNames(myGroup, strokes, objs){
var objsLength = objs.length,
names, l, k, m;
for (k = 0; k < objsLength; k++) {
names = objs[k]._name.split(' ');
if(strokes.length > names.length){
continue;
}
var strokesClone = strokes.concat();
for(l = 0; l < names.length; l++){
for(m = 0; m < strokesClone.length; m++){
if (names[l] === strokesClone[m]) {
strokesClone.splice(m, 1);
m--;
continue;
}
}
if(!strokesClone.length){
myGroup.elements.push(objs[k]);
break;
}
}
}
}
}
function findByCanvasAndLayer(i, j, myGroup) {
var objs = canvases[i].layers[j].objs,
grdntsnptrns = canvases[i].layers[j].grdntsnptrns,
objsLength = objs.length,
grdntsnptrnsLength = grdntsnptrns.length;
for (var k = 0; k < objsLength; k++)
myGroup.elements.push(objs[k]);
for (k = 0; k < grdntsnptrnsLength; k++)
myGroup.elements.push(grdntsnptrns[k]);
return myGroup;
}
var jCanvaScript = function (stroke, map) {
if (stroke === undefined)return this;
if (typeof stroke == 'object') {
map = stroke;
stroke = undefined;
}
var canvasNumber = -1, layerNumber = -1, limitC = canvases.length, myGroup = group(), i, j, canvas, layer, layers, element, limitL;
if (map === undefined) {
if (stroke.charAt(0) === '#') {
for (i = 0; i < limitC; i++) {
limitL = canvases[i].layers.length;
for (j = 0; j < limitL; j++) {
element = findById(i, j, stroke);
if (element)return element;
}
}
}
if (stroke.charAt(0) === '.') {
for (i = 0; i < limitC; i++) {
limitL = canvases[i].layers.length;
for (j = 0; j < limitL; j++)
myGroup = findByName(i, j, myGroup, stroke);
}
}
}
else {
if (map.canvas !== undefined) {
for (i = 0; i < limitC; i++)
if (canvases[i].optns.id == map.canvas) {
canvasNumber = i;
canvas = canvases[i];
break;
}
}
if (map.layer !== undefined) {
if (canvasNumber != -1) {
limitL = canvas.layers.length;
for (i = 0; i < limitL; i++)
if (canvas.layers[i].optns.id == map.layer) {
layerNumber = i;
layer = canvas.layers[i];
break;
}
}
else {
for (i = 0; i < limitC; i++) {
layers = canvases[i].layers;
limitL = layers.length;
for (j = 0; j < limitL; j++) {
if (layers[j].optns.id == map.layer) {
canvasNumber = i;
layerNumber = j;
canvas = canvases[i]
layer = canvas.layers[j];
break;
}
}
if (layer > -1)break;
}
}
}
if (layerNumber < 0 && canvasNumber < 0)return group();
if (layerNumber < 0) {
layers = canvas.layers;
limitL = layers.length;
if (stroke === undefined) {
for (j = 0; j < limitL; j++)
myGroup = findByCanvasAndLayer(canvasNumber, j, myGroup);
}
else if (stroke.charAt(0) === '#') {
for (j = 0; j < limitL; j++) {
element = findById(canvasNumber, j, stroke);
if (element)return element;
}
}
else if (stroke.charAt(0) === '.') {
for (j = 0; j < limitL; j++)
myGroup = findByName(canvasNumber, j, myGroup, stroke);
}
}
else {
if (stroke === undefined) {
myGroup = findByCanvasAndLayer(canvasNumber, layerNumber, myGroup);
}
if (stroke.charAt(0) === '#') {
return findById(canvasNumber, layerNumber, stroke);
}
if (stroke.charAt(0) === '.') {
myGroup = findByName(canvasNumber, layerNumber, myGroup, stroke)
}
}
}
if (map !== undefined)
if (map.attrs !== undefined || map.fns !== undefined)
return myGroup.find(map);
if (myGroup.elements.length)return myGroup;
return group();
}
<include src="privateFunctions"/>
<include src="privateObjects"/>
<include src="publicFunctions"/>
<include src="objects"/>
<include src="serviceObjects"/>
window.jCanvaScript = window.jc = jCanvaScript;
})(window, undefined);