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);
30function customLineHelp(ctx, style, path) {
31 var points = fromList(path);
32 if (path.closed) points.push(points[0]);
33 var pattern = fromList(style.dashing);
34 var i = points.length - 1;
36 var x0 = points[i]._0, y0 = points[i]._1;
37 var x1=0, y1=0, dx=0, dy=0, remaining=0, nx=0, ny=0;
38 var pindex = 0, plen = pattern.length;
39 var draw = true, segmentLength = pattern[0];
42 x1 = points[i]._0; y1 = points[i]._1;
43 dx = x1 - x0; dy = y1 - y0;
44 remaining = Math.sqrt(dx * dx + dy * dy);
45 while (segmentLength <= remaining) {
46 x0 += dx * segmentLength / remaining;
47 y0 += dy * segmentLength / remaining;
48 ctx[draw ? 'lineTo' : 'moveTo'](x0, y0);
49 // update starting position
50 dx = x1 - x0; dy = y1 - y0;
51 remaining = Math.sqrt(dx * dx + dy * dy);
54 pindex = (pindex + 1) % plen;
55 segmentLength = pattern[pindex];
58 ctx[draw ? 'lineTo' : 'moveTo'](x1, y1);
59 segmentLength -= remaining;
65function drawLine(ctx, style, path) {
66 ctx.lineWidth = style.width;
67 var cap = style.cap.ctor;
68 ctx.lineCap = cap === 'Flat' ? 'butt' :
69 cap === 'Round' ? 'round' : 'square';
70 var join = style.join.ctor;
71 ctx.lineJoin = join === 'Smooth' ? 'round' :
72 join === 'Sharp' ? 'miter' : 'bevel';
73 ctx.miterLimit = style.join._0 || 10;
74 ctx.strokeStyle = extract(style.color);
75 return line(ctx, style, path);
78function texture(redo, ctx, src) {
79 var img = new Image();
80 img.src = fromString(src);
82 return ctx.createPattern(img, 'repeat');
85function gradient(ctx, grad) {
88 if (grad.ctor === 'Linear') {
89 var p0 = grad._0, p1 = grad._1;
90 g = ctx.createLinearGradient(p0._0, -p0._1, p1._0, -p1._1);
91 stops = fromList(grad._2);
93 var p0 = grad._0, p2 = grad._2;
94 g = ctx.createRadialGradient(p0._0, -p0._1, grad._1, p2._0, -p2._1, grad._3);
95 stops = fromList(grad._4);
97 var len = stops.length;
98 for (var i = 0; i < len; ++i) {
100 g.addColorStop(stop._0, extract(stop._1));
105function drawShape(redo, ctx, style, path) {
107 var sty = style.ctor;
109 sty === 'Solid' ? extract(style._0) :
110 sty === 'Texture' ? texture(redo, ctx, style._0) : gradient(ctx, style._0);
115function drawImage(redo, ctx, form) {
116 var img = new Image();
118 img.src = fromString(form._3);
132 ctx.drawImage(img, srcX, srcY, srcW, srcH, destX, destY, destW, destH);
135function renderForm(redo, ctx, form) {
137 var x = form.x, y = form.y, theta = form.theta, scale = form.scale;
138 if (x !== 0 || y !== 0) ctx.translate(x, y);
139 if (theta !== 0) ctx.rotate(theta);
140 if (scale !== 1) ctx.scale(scale,scale);
144 case 'FPath' : drawLine(ctx, f._0, f._1); break;
145 case 'FImage': drawImage(redo, ctx, f); break;
147 if (f._0.ctor === 'Left') {
149 drawLine(ctx, f._0._0, f._1);
151 drawShape(redo, ctx, f._0._0, f._1);
158function formToMatrix(form) {
159 var scale = form.scale;
160 var matrix = A6( Matrix.matrix, scale, 0, 0, scale, form.x, form.y );
162 var theta = form.theta
164 matrix = A2( Matrix.multiply, matrix, Matrix.rotation(theta) );
169function makeTransform(w, h, form, matrices) {
170 var props = form.form._0.props;
171 var m = A6( Matrix.matrix, 1, 0, 0, 1,
173 (h - props.height)/2 );
174 var len = matrices.length;
175 for (var i = 0; i < len; ++i) { m = A2( Matrix.multiply, m, matrices[i] ); }
176 m = A2( Matrix.multiply, m, formToMatrix(form) );
178 return 'matrix(' + m[0] + ',' + m[3] + ',' +
179 (-m[1]) + ',' + (-m[4]) + ',' +
180 m[2] + ',' + m[5] + ')';
183function stepperHelp(list) {
184 var arr = fromList(list);
186 function peekNext() {
187 return i < arr.length ? arr[i].form.ctor : '';
189 // assumes that there is a next element
195 return { peekNext:peekNext, next:next };
198function stepper(forms) {
199 var ps = [stepperHelp(forms)];
201 function peekNext() {
204 for (var i = 0; i < len; ++i ) {
205 if (formType = ps[i].peekNext()) return formType;
209 // assumes that there is a next element
211 while (!ps[0].peekNext()) { ps.shift(); matrices.pop(); if (ctx) { ctx.restore(); } }
212 var out = ps[0].next();
214 if (f.ctor === 'FGroup') {
215 ps.unshift(stepperHelp(f._1));
216 var m = A2( Matrix.multiply, f._0, formToMatrix(out));
218 ctx.transform(m[0], m[3], m[1], m[4], m[2], m[5]);
223 function transforms() { return matrices; }
224 return { peekNext:peekNext, next:next, transforms:transforms };
227function makeCanvas(w,h) {
228 var canvas = newElement('canvas');
229 canvas.style.width = w + 'px';
230 canvas.style.height = h + 'px';
231 canvas.style.display = "block";
232 canvas.style.position = "absolute";
238function render(model) {
239 var div = newElement('div');
240 update(div, model, model);
244function updateTracker(w,h,div) {
245 var kids = div.childNodes;
247 function transform(transforms, ctx) {
248 ctx.translate(w/2, h/2);
250 var len = transforms.length;
251 for (var i = 0; i < len; ++i) {
252 var m = transforms[i];
254 ctx.transform(m[0], m[3], m[1], m[4], m[2], m[5]);
258 function getContext(transforms) {
259 while (i < kids.length) {
261 if (node.getContext) {
264 node.style.width = w + 'px';
265 node.style.height = h + 'px';
267 return transform(transforms, node.getContext('2d'));
269 div.removeChild(node);
271 var canvas = makeCanvas(w,h);
272 div.appendChild(canvas);
273 // we have added a new node, so we must step our position
275 return transform(transforms, canvas.getContext('2d'));
277 function element(matrices, form) {
278 var container = kids[i];
279 if (!container || container.getContext) {
280 container = newElement('div');
281 container.style.overflow = 'hidden';
282 container.style.position = 'absolute';
283 addTransform(container.style, 'scaleY(-1)');
286 kid ? div.insertBefore(container, kid)
287 : div.appendChild(container);
289 // we have added a new node, so we must step our position
292 container.style.width = w + 'px';
293 container.style.height = h + 'px';
295 var elem = form.form._0;
296 var node = container.firstChild;
298 Render.update(node, node.oldElement, elem);
299 node = container.firstChild;
301 node = Render.render(elem);
302 container.appendChild(node);
304 node.oldElement = elem;
305 addTransform(node.style, makeTransform(w, h, form, matrices));
307 function clearRest() {
308 while (i < kids.length) {
309 div.removeChild(kids[i]);
312 return { getContext:getContext, element:element, clearRest:clearRest };
316function update(div, _, model) {
319 div.style.width = w + 'px';
320 div.style.height = h + 'px';
321 if (model.forms.ctor === 'Nil') {
322 while (div.hasChildNodes()) {
323 div.removeChild(div.lastChild);
326 var stpr = stepper(model.forms);
327 var tracker = updateTracker(w,h,div);
331 while (formType = stpr.peekNext()) {
332 if (ctx === null && formType !== 'FElement') {
333 ctx = tracker.getContext(stpr.transforms());
335 var form = stpr.next(ctx);
336 if (formType === 'FElement') {
337 tracker.element(stpr.transforms(), form);
339 } else if (formType !== 'FGroup') {
340 renderForm(function() { update(div, model, model); }, ctx, form);
347return { 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