2var Collage = function() {
4var JS = Elm.JavaScript;
6function tracePoints(ctx,points) {
7 var i = points.length - 1;
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.Graphics.Color.extract(color);
19function filled(ctx,color,points) {
20 tracePoints(ctx,points);
21 ctx.fillStyle = Elm.Graphics.Color.extract(color);
25function textured(redo,ctx,src,points) {
26 var img = new Image();
27 img.src = JS.castStringToJSString(src);
30 tracePoints(ctx,points);
31 ctx.fillStyle = ctx.createPattern(img,'repeat');
35function customLine(pattern,ctx,color,points) {
36 if (pattern.length === 0) { pattern = [8,4]; }
37 customLineHelp(ctx, pattern, points);
38 ctx.strokeStyle = Elm.Graphics.Color.extract(color);
42var customLineHelp = function(ctx, pattern, points) {
43 var i = points.length - 1;
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];
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);
63 pindex = (pindex + 1) % plen;
64 segmentLength = pattern[pindex];
67 ctx[draw ? 'lineTo' : 'moveTo'](x1, y1);
68 segmentLength -= remaining;
74function drawLine(ctx,form) {
75 var points = form[3][1];
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);
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);
90 return customLine(shapeStyle[1],ctx,color,points);
94function drawImage(redo,ctx,w,h,src) {
95 var img = new Image();
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) {
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);
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;
115function renderForms(redo,ctx,w,h,forms) {
116 ctx.clearRect(0,0,w,h);
117 for (var i = forms.length; i--; ) {
119 renderForm(redo,ctx,f[1],f[2],f[3][1],f[3][2],f[4]);
123function collageForms(w,h,forms) {
124 var canvas = Render.newElement('canvas');
125 canvas.style.width = (~~w) + 'px';
126 canvas.style.height = (~~h) + 'px';
127 canvas.style.display = "block";
130 if (canvas.getContext) {
131 var ctx = canvas.getContext('2d');
132 var w = canvas.width, h = canvas.height;
133 function redo() { renderForms(this,ctx,w,h,forms); }
134 renderForms(redo,ctx,w,h,forms);
137 canvas.innerHTML = "Your browser does not support the canvas element.";
141function collageElement(w,h,theta,scale,x,y,elem) {
142 var e = Render.render(elem);
143 var t = "translate(" + (x - elem[3] / 2) + "px,"+ (y - elem[4] / 2) + "px)";
144 var r = theta === (~~theta) ? "" : "rotate(" + theta*360 + "deg)";
145 var s = scale === 1 ? "" : "scale(" + scale + "," + scale + ")";
146 var transforms = t + " " + s + " " + r;
147 e.style.transform = transforms;
148 e.style.msTransform = transforms;
149 e.style.MozTransform = transforms;
150 e.style.webkitTransform = transforms;
151 e.style.OTransform = transforms;
152 var div = Render.newElement('div');
154 div.style.width = (~~w) + "px";
155 div.style.height = (~~h) + "px";
156 div.style.overflow = "hidden";
160function collage(w,h,formss) {
161 if (formss.length === 0) { return collageForms(w,h,[]); }
162 var elems = new Array(formss.length);
163 for (var i = formss.length; i--; ) {
165 if (typeof f[0] === "string") {
166 elems[i] = collageElement(w,h,f[1],f[2],f[3][1],f[3][2],f[4][1]);
168 elems[i] = collageForms(w,h,f);
171 if (formss.length === 1) { return elems[0]; }
172 return Render.flowWith(Render.goIn,function(x){return x},elems);
175function updateFormSet(node,currSet,nextSet) {
176 if (Value.eq(nextSet,currSet)) return;
177 var w = node.style.width.slice(0,-2) - 0;
178 var h = node.style.height.slice(0,-2) - 0;
179 if (typeof nextSet[0] === "object") {
180 if (typeof currSet[0] === "object") {
181 if (node.getContext) {
182 var ctx = node.getContext('2d');
183 function redo() { renderForms(this,ctx,w,h,nextSet); }
184 return renderForms(redo,ctx,w,h,nextSet);
187 var newNode = collageForms(w,h,nextSet);
188 newNode.style.position = 'absolute';
189 return node.parentNode.replaceChild(newNode,node);
192 var newNode = collageElement(w,h,f[1],f[2],f[3][1],f[3][2],f[4][1]);
193 newNode.style.position = 'absolute';
194 return node.parentNode.replaceChild(newNode,node);
197// assumes that the form sets are the same length.
198function updateCollage(node,currs,nexts) {
199 if (nexts.length === 1) {
200 return updateFormSet(node,currs[0],nexts[0]);
202 var kids = node.childNodes;
203 var len = kids.length;
204 for (var i = len; i--; ) {
205 updateFormSet(kids[len-i-1], currs[i], nexts[i]);
209function style(clr,n,list) {
211 '<span style="font-size:100%;color:' + clr + ';">' + n + '</span>',
215function insideForm(point) { return function(form) {
216 if (!inBoundsOf(point[1],point[2],form)) return false;
218 switch (form[4][0]) {
219 case "FShape": return insideShape(point,form[1],form[2],form[3],form[4][3][1]);
220 case "FLine": return false;
226 hw = form[4][1][3] / 2;
227 hh = form[4][1][4] / 2;
230 return insideShape(point,form[1],form[2],form[3],
239function inBoundsOf(px,py,form) {
240 if (form.length < 6) {
241 var fx = form[3][1], fy = form[3][2];
242 var radiusSquared = 0;
244 switch (form[4][0]) {
246 var points = form[4][3][1];
247 for (var i = points.length; --i; ) {
249 radiusSquared = Math.max(radiusSquared, p[1]*p[1] + p[2]*p[2]);
251 radiusSquared *= scale * scale;
256 var x = scale * form[4][1] / 2;
257 var y = scale * form[4][2] / 2;
258 radiusSquared = x*x + y*y;
261 var x = scale * form[4][1][3] / 2;
262 var y = scale * form[4][1][4] / 2;
263 radiusSquared = x*x + y*y;
266 form.push(function(px,py) {
269 return dx*dx + dy*dy < radiusSquared + 1;
272 return form[5](px,py);
275function insideShape(point,theta,scale,pos,points) {
280 var x = (point[1] - pos[1]) / scale;
281 var y = (point[2] - pos[2]) / scale;
283 var t = -2 * Math.PI * theta;
284 var nx = x * Math.cos(t) - y * Math.sin(t);
285 y = x * Math.sin(t) + y * Math.cos(t);
289 if (points.length === 0) { return false; }
291 for (var i = points.length - 1; i--; ) {
293 var p1x = p1[1], p1y = p1[2], p2x = p2[1], p2y = p2[2];
295 if (p1y < p2y) {var ymin=p1y, ymax=p2y;} else {var ymin=p2y, ymax=p1y;}
296 if (p1x < p2x) {var xmin=p1x, xmax=p2x;} else {var xmin=p2x, xmax=p1x;}
298 if (ymin < y && y <= ymax && x <= xmax) {
299 if (x <= xmin || x <= ((y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x)) {
305 return (counter % 2) === 1;
308return {collage:collage, updateCollage:updateCollage, insideForm:insideForm};
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list