on d15e080 help sign in
elk/core-js/Graphics/Collage.js
311 lines 9.0 kB history raw
2var Collage = function() {
4var JS = Elm.JavaScript;
6function tracePoints(ctx,points) {
7 var i = points.length - 1;
8 if (i <= 0) return;
9 ctx.moveTo(points[i][1], points[i][2]);
10 while (i--) { ctx.lineTo(points[i][1], points[i][2]); }
13function solid(ctx,color,points) {
14 tracePoints(ctx,points);
15 ctx.strokeStyle = Elm.Color.extract(color);
16 ctx.stroke();
17};
19function filled(ctx,color,points) {
20 tracePoints(ctx,points);
21 ctx.fillStyle = Elm.Color.extract(color);
22 ctx.fill();
25function textured(redo,ctx,src,points) {
26 var img = new Image();
27 img.src = JS.castStringToJSString(src);
28 img.onload = redo;
30 tracePoints(ctx,points);
31 ctx.fillStyle = ctx.createPattern(img,'repeat');
32 ctx.fill();
35function customLine(pattern,ctx,color,points) {
36 if (pattern.length === 0) { pattern = [8,4]; }
37 customLineHelp(ctx, pattern, points);
38 ctx.strokeStyle = Elm.Color.extract(color);
39 ctx.stroke();
40};
42var customLineHelp = function(ctx, pattern, points) {
43 var i = points.length - 1;
44 if (i <= 0) return;
45 var x0 = points[i][1], y0 = points[i][2];
46 var x1=0, y1=0, dx=0, dy=0, remaining=0, nx=0, ny=0;
47 var pindex = 0, plen = pattern.length;
48 var draw = true, segmentLength = pattern[0];
49 ctx.moveTo(x0,y0);
50 while (i--) {
51 x1 = points[i][1]; y1 = points[i][2];
52 dx = x1 - x0; dy = y1 - y0;
53 remaining = Math.sqrt(dx * dx + dy * dy);
54 while (segmentLength <= remaining) {
55 x0 += dx * segmentLength / remaining;
56 y0 += dy * segmentLength / remaining;
57 ctx[draw ? 'lineTo' : 'moveTo'](x0, y0);
58 // update starting position
59 dx = x1 - x0; dy = y1 - y0;
60 remaining = Math.sqrt(dx * dx + dy * dy);
61 // update pattern
62 draw = !draw;
63 pindex = (pindex + 1) % plen;
64 segmentLength = pattern[pindex];
65 }
66 if (remaining > 0) {
67 ctx[draw ? 'lineTo' : 'moveTo'](x1, y1);
68 segmentLength -= remaining;
69 }
70 x0 = x1; y0 = y1;
71 }
72};
74function drawLine(ctx,form) {
75 var points = form[3][1];
76 switch(form[1][0]) {
77 case "Solid" : return solid(ctx,form[2],points);
78 case "Dotted": return customLine([3,3],ctx,form[2],points);
79 case "Dashed": return customLine([8,4],ctx,form[2],points);
80 case "Custom": return customLine(form[1][1],ctx,form[2],points);
81 }
82};
84function drawShape(redo,ctx,shapeStyle,color,points) {
85 switch(shapeStyle[0]) {
86 case "Filled": return filled(ctx,color,points);
87 case "Outlined": return solid(ctx,color,points);
88 case "Textured": return textured(redo,ctx,shapeStyle[1],points);
89 case "CustomOutline":
90 return customLine(shapeStyle[1],ctx,color,points);
91 }
92};
94function drawImage(redo,ctx,w,h,src) {
95 var img = new Image();
96 img.onload = redo;
97 img.src = JS.castStringToJSString(src);
98 ctx.drawImage(img,-w/2,-h/2,w,h);
101function renderForm(redo,ctx,theta,scale,x,y,form) {
102 ctx.save();
103 if (x !== 0 || y !== 0) ctx.translate(x,y);
104 if (theta !== ~~theta) ctx.rotate(2*Math.PI*theta);
105 if (scale !== 1) ctx.scale(scale,scale);
106 ctx.beginPath();
107 switch(form[0]) {
108 case "FLine": drawLine(ctx,form); break;
109 case "FShape": drawShape(redo,ctx,form[1],form[2],form[3][1]); break;
110 case "FImage": drawImage(redo,ctx,form[1],form[2],form[3]); break;
111 }
112 ctx.restore();
113};
115function renderForms(redo,ctx,w,h,forms) {
116 ctx.clearRect(0,0,w,h);
117 for (var i = forms.length; i--; ) {
118 var f = forms[i];
119 renderForm(redo,ctx,f[1],f[2],f[3][1],f[3][2],f[4]);
120 }
123function collageForms(w,h,forms) {
124 var canvas = Render.newElement('canvas');
125 w = ~~w;
126 h = ~~h;
127 canvas.style.width = w + 'px';
128 canvas.style.height = h + 'px';
129 canvas.style.display = "block";
130 canvas.width = w;
131 canvas.height = h;
132 if (canvas.getContext) {
133 var ctx = canvas.getContext('2d');
134 function redo() { renderForms(this,ctx,w,h,forms); }
135 renderForms(redo,ctx,w,h,forms);
136 return canvas;
137 }
138 canvas.innerHTML = "Your browser does not support the canvas element.";
139 return canvas;
140};
142function collageElement(w,h,theta,scale,x,y,elem) {
143 var e = Render.render(elem);
144 var t = "translate(" + (x - elem[3] / 2) + "px,"+ (y - elem[4] / 2) + "px)";
145 var r = theta === (~~theta) ? "" : "rotate(" + theta*360 + "deg)";
146 var s = scale === 1 ? "" : "scale(" + scale + "," + scale + ")";
147 var transforms = t + " " + s + " " + r;
148 e.style.transform = transforms;
149 e.style.msTransform = transforms;
150 e.style.MozTransform = transforms;
151 e.style.webkitTransform = transforms;
152 e.style.OTransform = transforms;
153 var div = Render.newElement('div');
154 Render.addTo(div,e);
155 div.style.width = (~~w) + "px";
156 div.style.height = (~~h) + "px";
157 div.style.overflow = "hidden";
158 return div;
161function collage(w,h,formss) {
162 if (formss.length === 0) { return collageForms(w,h,[]); }
163 var elems = new Array(formss.length);
164 for (var i = formss.length; i--; ) {
165 var f = formss[i];
166 if (typeof f[0] === "string") {
167 elems[i] = collageElement(w,h,f[1],f[2],f[3][1],f[3][2],f[4][1]);
168 } else {
169 elems[i] = collageForms(w,h,f);
170 }
171 }
172 if (formss.length === 1) { return elems[0]; }
173 return Render.flowWith(Render.goIn,function(x){return x},elems);
176function updateFormSet(node,currSet,nextSet) {
177 if (Value.eq(nextSet,currSet)) return;
178 var w = node.style.width.slice(0,-2) - 0;
179 var h = node.style.height.slice(0,-2) - 0;
180 if (typeof nextSet[0] === "object") {
181 if (typeof currSet[0] === "object") {
182 if (node.getContext) {
183 var ctx = node.getContext('2d');
184 function redo() { renderForms(this,ctx,w,h,nextSet); }
185 return renderForms(redo,ctx,w,h,nextSet);
186 }
187 }
188 var newNode = collageForms(w,h,nextSet);
189 newNode.style.position = 'absolute';
190 return node.parentNode.replaceChild(newNode,node);
191 }
192 var f = nextSet;
193 var newNode = collageElement(w,h,f[1],f[2],f[3][1],f[3][2],f[4][1]);
194 newNode.style.position = 'absolute';
195 return node.parentNode.replaceChild(newNode,node);
198// assumes that the form sets are the same length.
199function updateCollage(node,currs,nexts) {
200 if (nexts.length === 1) {
201 return updateFormSet(node,currs[0],nexts[0]);
202 }
203 var kids = node.childNodes;
204 var len = kids.length;
205 for (var i = len; i--; ) {
206 updateFormSet(kids[len-i-1], currs[i], nexts[i]);
207 }
210function style(clr,n,list) {
211 return ["Tuple2",
212 '<span style="font-size:100%;color:' + clr + ';">' + n + '</span>',
213 list];
216function insideForm(point) { return function(form) {
217 if (!inBoundsOf(point[1],point[2],form)) return false;
218 var hw, hh;
219 switch (form[4][0]) {
220 case "FShape": return insideShape(point,form[1],form[2],form[3],form[4][3][1]);
221 case "FLine": return false;
222 case "FImage":
223 hw = form[4][1] / 2;
224 hh = form[4][2] / 2;
225 break;
226 case "FElement":
227 hw = form[4][1][3] / 2;
228 hh = form[4][1][4] / 2;
229 break;
230 }
231 return insideShape(point,form[1],form[2],form[3],
232 [ [null, hw, hh],
233 [null,-hw, hh],
234 [null,-hw,-hh],
235 [null, hw,-hh],
236 [null, hw, hh] ]);
237 };
240function inBoundsOf(px,py,form) {
241 if (form.length < 6) {
242 var fx = form[3][1], fy = form[3][2];
243 var radiusSquared = 0;
244 var scale = form[2];
245 switch (form[4][0]) {
246 case "FShape":
247 var points = form[4][3][1];
248 for (var i = points.length; --i; ) {
249 var p = points[i];
250 radiusSquared = Math.max(radiusSquared, p[1]*p[1] + p[2]*p[2]);
251 }
252 radiusSquared *= scale * scale;
253 break;
254 case "FLine":
255 break;
256 case "FImage":
257 var x = scale * form[4][1] / 2;
258 var y = scale * form[4][2] / 2;
259 radiusSquared = x*x + y*y;
260 break;
261 case "FElement":
262 var x = scale * form[4][1][3] / 2;
263 var y = scale * form[4][1][4] / 2;
264 radiusSquared = x*x + y*y;
265 break;
266 }
267 form.push(function(px,py) {
268 var dx = px - fx;
269 var dy = py - fy;
270 return dx*dx + dy*dy < radiusSquared + 1;
271 });
272 }
273 return form[5](px,py);
276function insideShape(point,theta,scale,pos,points) {
277 var counter = 0;
278 var list = ["Nil"];
279 var p1,p2;
281 var x = (point[1] - pos[1]) / scale;
282 var y = (point[2] - pos[2]) / scale;
283 if (theta !== 0) {
284 var t = -2 * Math.PI * theta;
285 var nx = x * Math.cos(t) - y * Math.sin(t);
286 y = x * Math.sin(t) + y * Math.cos(t);
287 x = nx;
288 }
290 if (points.length === 0) { return false; }
291 p1 = points[0];
292 for (var i = points.length - 1; i--; ) {
293 p2 = points[i];
294 var p1x = p1[1], p1y = p1[2], p2x = p2[1], p2y = p2[2];
296 if (p1y < p2y) {var ymin=p1y, ymax=p2y;} else {var ymin=p2y, ymax=p1y;}
297 if (p1x < p2x) {var xmin=p1x, xmax=p2x;} else {var xmin=p2x, xmax=p1x;}
299 if (ymin < y && y <= ymax && x <= xmax) {
300 if (x <= xmin || x <= ((y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x)) {
301 ++counter;
302 }
303 }
304 p1 = p2;
305 }
306 return (counter % 2) === 1;
309return {collage:collage, updateCollage:updateCollage, insideForm:insideForm};
311}();