2ElmRuntime.Render.Collage = function() {
5var Render = ElmRuntime.use(ElmRuntime.Render.Element);
6var Matrix = Elm.Matrix2D({});
7var Utils = ElmRuntime.use(ElmRuntime.Render.Utils);
8var newElement = Utils.newElement,
9 extract = Utils.extract, fromList = Utils.fromList,
10 fromString = Utils.fromString, addTransform = Utils.addTransform;
12function trace(ctx, path) {
13 var points = fromList(path);
14 var i = points.length - 1;
16 ctx.moveTo(points[i]._0, points[i]._1);
17 while (i--) { ctx.lineTo(points[i]._0, points[i]._1); }
19 i = points.length - 1;
20 ctx.lineTo(points[i]._0, points[i]._1);
24function line(ctx,style,path) {
25 style.dashing.ctor === 'Nil' ? trace(ctx, path) : customLineHelp(ctx, style, path);
29function customLineHelp(ctx, style, path) {
30 var points = fromList(path);
31 if (path.closed) points.push(points[0]);
32 var pattern = fromList(style.dashing);
33 var i = points.length - 1;
35 var x0 = points[i]._0, y0 = points[i]._1;
36 var x1=0, y1=0, dx=0, dy=0, remaining=0, nx=0, ny=0;
37 var pindex = 0, plen = pattern.length;
38 var draw = true, segmentLength = pattern[0];
41 x1 = points[i]._0; y1 = points[i]._1;
42 dx = x1 - x0; dy = y1 - y0;
43 remaining = Math.sqrt(dx * dx + dy * dy);
44 while (segmentLength <= remaining) {
45 x0 += dx * segmentLength / remaining;
46 y0 += dy * segmentLength / remaining;
47 ctx[draw ? 'lineTo' : 'moveTo'](x0, y0);
48 // update starting position
49 dx = x1 - x0; dy = y1 - y0;
50 remaining = Math.sqrt(dx * dx + dy * dy);
53 pindex = (pindex + 1) % plen;
54 segmentLength = pattern[pindex];
57 ctx[draw ? 'lineTo' : 'moveTo'](x1, y1);
58 segmentLength -= remaining;
64function drawLine(ctx, style, path) {
65 ctx.lineWidth = style.width;
66 var cap = style.cap.ctor;
67 ctx.lineCap = cap === 'Flat' ? 'butt' :
68 cap === 'Round' ? 'round' : 'square';
69 var join = style.join.ctor;
70 ctx.lineJoin = join === 'Smooth' ? 'round' :
71 join === 'Sharp' ? 'miter' : 'bevel';
72 ctx.miterLimit = style.join._0 || 10;
73 ctx.strokeStyle = extract(style.color);
74 return line(ctx, style, path);
77function texture(redo, ctx, src) {
78 var img = new Image();
79 img.src = fromString(src);
81 return ctx.createPattern(img, 'repeat');
84function gradient(ctx, grad) {
87 if (grad.ctor === 'Linear') {
88 var p0 = grad._0, p1 = grad._1;
89 g = ctx.createLinearGradient(p0._0, -p0._1, p1._0, -p1._1);
90 stops = fromList(grad._2);
92 var p0 = grad._0, p2 = grad._2;
93 g = ctx.createRadialGradient(p0._0, -p0._1, grad._1, p2._0, -p2._1, grad._3);
94 stops = fromList(grad._4);
96 var len = stops.length;
97 for (var i = 0; i < len; ++i) {
99 g.addColorStop(stop._0, extract(stop._1));
104function drawShape(redo, ctx, style, path) {
106 var sty = style.ctor;
108 sty === 'Solid' ? extract(style._0) :
109 sty === 'Texture' ? texture(redo, ctx, style._0) : gradient(ctx, style._0);
113function drawImage(redo, ctx, form) {
114 var img = new Image();
116 img.src = fromString(form._3);
129 ctx.drawImage(img, srcX, srcY, srcW, srcH, destX, destY, destW, destH);
132function renderForm(redo, ctx, form) {
134 var x = form.x, y = form.y, theta = form.theta, scale = form.scale;
135 if (x !== 0 || y !== 0) ctx.translate(x, y);
136 if (theta !== 0) ctx.rotate(theta);
137 ctx.scale(scale,-scale);
141 case 'FPath' : drawLine(ctx, f._0, f._1); break;
142 case 'FImage': drawImage(redo, ctx, f); break;
144 if (f._0.ctor === 'Left') {
146 drawLine(ctx, f._0._0, f._1);
148 drawShape(redo, ctx, f._0._0, f._1);
155function formToMatrix(form) {
156 var scale = form.scale;
157 var matrix = A6( Matrix.matrix, scale, 0, 0, scale, scale * form.x, scale * form.y );
159 var theta = form.theta
161 matrix = A2( Matrix.multiply, matrix, Matrix.rotation(theta) );
166function makeTransform(w, h, form, matrices) {
167 var props = form.form._0.props;
168 var m = A6( Matrix.matrix, 1, 0, 0, 1,
170 (h - props.height)/2 );
171 var len = matrices.length;
172 for (var i = 0; i < len; ++i) { m = A2( Matrix.multiply, m, matrices[i] ); }
173 m = A2( Matrix.multiply, m, formToMatrix(form) );
175 return 'matrix(' + m[0] + ',' + m[3] + ',' +
176 (-m[1]) + ',' + (-m[4]) + ',' +
177 m[2] + ',' + m[5] + ')';
180function stepperHelp(list) {
181 var arr = fromList(list);
183 function peekNext() {
184 return i < arr.length ? arr[i].form.ctor : '';
186 // assumes that there is a next element
192 return { peekNext:peekNext, next:next };
195function stepper(forms) {
196 var ps = [stepperHelp(forms)];
198 function peekNext() {
201 for (var i = 0; i < len; ++i ) {
202 if (formType = ps[i].peekNext()) return formType;
206 // assumes that there is a next element
208 while (!ps[0].peekNext()) { ps.shift(); matrices.pop(); ctx.restore(); }
209 var out = ps[0].next();
211 if (f.ctor === 'FGroup') {
212 ps.unshift(stepperHelp(f._1));
213 var m = A2( Matrix.multiply, f._0, formToMatrix(out));
215 ctx.transform(m[0], m[3], m[1], m[4], m[2], m[5]);
220 function transforms() { return matrices; }
221 return { peekNext:peekNext, next:next, transforms:transforms };
224function makeCanvas(w,h) {
225 var canvas = newElement('canvas');
226 canvas.style.width = w + 'px';
227 canvas.style.height = h + 'px';
228 canvas.style.display = "block";
229 canvas.style.position = "absolute";
235function render(model) {
236 var div = newElement('div');
237 update(div, model, model);
241function updateTracker(w,h,div) {
242 var kids = div.childNodes;
244 function transform(transforms, ctx) {
245 ctx.translate(w/2, h/2);
246 var len = transforms.length;
247 for (var i = 0; i < len; ++i) {
248 var m = transforms[i];
250 ctx.transform(m[0], m[3], m[1], m[4], m[2], m[5]);
254 function getContext(transforms) {
255 while (i < kids.length) {
256 var node = kids[i++];
257 if (node.getContext) {
260 node.style.width = w + 'px';
261 node.style.height = h + 'px';
262 return transform(transforms, node.getContext('2d'));
264 div.removeChild(node);
266 var canvas = makeCanvas(w,h);
267 div.appendChild(canvas);
268 // we have added a new node, so we must step our position
270 return transform(transforms, canvas.getContext('2d'));
272 function element(matrices, form) {
273 var container = kids[i];
274 if (!container || container.getContext) {
275 container = newElement('div');
276 container.style.overflow = 'hidden';
277 container.style.position = 'absolute';
278 addTransform(container.style, 'scaleY(-1)');
280 div.appendChild(container);
284 div.insertBefore(container, kid);
286 div.appendChild(container);
290 // we have added a new node, so we must step our position
293 container.style.width = w + 'px';
294 container.style.height = h + 'px';
296 var elem = form.form._0;
297 var node = container.firstChild;
299 Render.update(node, node.oldElement, elem);
300 node = container.firstChild;
302 node = Render.render(elem);
303 container.appendChild(node);
305 node.oldElement = elem;
306 addTransform(node.style, makeTransform(w, h, form, matrices));
308 return { getContext:getContext, element:element };
312function update(div, _, model) {
315 div.style.width = w + 'px';
316 div.style.height = h + 'px';
317 if (model.forms.ctor === 'Nil') {
318 while (div.hasChildNodes()) {
319 div.removeChild(div.lastChild);
322 var stpr = stepper(model.forms);
323 var tracker = updateTracker(w,h,div);
327 while (formType = stpr.peekNext()) {
328 if (ctx === null && formType !== 'FElement') {
329 ctx = tracker.getContext(stpr.transforms());
332 var form = stpr.next(ctx);
333 if (formType === 'FElement') {
334 tracker.element(stpr.transforms(), form);
336 } else if (formType !== 'FGroup') {
337 renderForm(function() { update(div, model, model); }, ctx, form);
343return { render:render, update:update };
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