on master help sign in
Get some basic collage functions working: forms excluding embedded elements, rect is known to work.
commit212543f51a90c9bedde0821069c3b7ab39371e68 authorevancz <info@elm-lang.org> dateMon 18 Mar 2013, 02:53 (14 years ago) parentf7f137f
6 files changed+72−43
libraries/Graphics/Geometry.elm+5−3 libraries/Graphics/LineStyle.elm+2−2 libraries/Native/Graphics/Collage.js+19 libraries/Native/Graphics/Matrix.js+1−1 runtime/Init.js+34−32 runtime/Render/Collage.js+11−5
libraries/Graphics/Geometry.elm+5−3
@@ -2,7 +2,9 @@
22-- These structures are purely geometric forms, no colors or styles.
33-- Could be the basis of some cool geometry stuff :)
44
5−module Geometry where
5+module Graphics.Geometry where
6+
7+import List
68
79type Path = [(Float,Float)]
810
@@ -17,12 +19,12 @@ rect w h = [ (0-w/2,0-h/2), (0-w/2,h/2), (w/2,h/2), (w/2,0-h/2) ]
1719
1820oval w h =
1921 let n = 50
20− f i = (w/2 * cos (2*pi/n * i), h/2 * sin (2*pi/n * i))
22+ f i = (w/2 * Math.cos (2*Math.pi/n * i), h/2 * Math.sin (2*Math.pi/n * i))
2123 in map f [0..n-1]
2224
2325circle r = oval (2*r) (2*r)
2426
2527ngon n r =
2628 let m = toFloat n
27− f i = ( r * cos (2*pi/m * i), r * sin (2*pi/m * i))
29+ f i = ( r * Math.cos (2*Math.pi/m * i), r * Math.sin (2*Math.pi/m * i))
2830 in map f [0..n-1]
libraries/Graphics/LineStyle.elm+2−2
@@ -17,10 +17,10 @@ type LineStyle = {
1717 }
1818
1919default = {
20− color = black,
20+ color = Color.black,
2121 width = 1,
2222 cap = Butt,
23− join = Miter,
23+ join = Sharp,
2424 dashing = [],
2525 dashOffset = 0,
2626 miterLimit = 10
libraries/Native/Graphics/Collage.jsnew file+19
@@ -0,0 +1,19 @@
1+
2+Elm.Native.Graphics.Collage = function(elm) {
3+ "use strict";
4+
5+ elm.Native = elm.Native || {};
6+ elm.Native.Graphics = elm.Native.Graphics || {};
7+ if (elm.Native.Graphics.Collage) return elm.Native.Graphics.Collage;
8+
9+ var newElement = Elm.Graphics.Element(elm).newElement;
10+ var render = ElmRuntime.use(ElmRuntime.Render.Collage).render;
11+
12+ function collage(w,h,forms) {
13+ return A3(newElement, w, h,
14+ {ctor: 'Custom', render: render,
15+ model: {w:w, h:h, forms:forms} });
16+ }
17+ return elm.Native.Graphics.Collage = { collage:F3(collage) };
18+
19+};
libraries/Native/Graphics/Matrix.js+1−1
@@ -35,7 +35,7 @@ Elm.Native.Graphics.Matrix = function(elm) {
3535
3636 return elm.Native.Graphics.Matrix = {
3737 identity:identity,
38− rotation:F2(rotation),
38+ rotate:F2(rotate),
3939 scale:F3(scale),
4040 move:F3(move),
4141 matrix:F7(matrix)
runtime/Init.js+34−32
@@ -42,51 +42,53 @@ Elm.init = function(module, baseNode) {
4242 // object. This permits many Elm programs to be embedded per document.
4343 var elm = {notify: notify, node: baseNode, id: ElmRuntime.guid(), inputs: inputs};
4444
45− // If graphics are enabled, set up the `main` value in baseNode.
46− if (baseNode !== null) {
47− var Render = ElmRuntime.use(ElmRuntime.Render.Element);
45+ // Set up methods to communicate with Elm program from JS.
46+ function send(name, value) {
47+ var e = document.createEvent('Event');
48+ e.initEvent(name + '_' + elm.id, true, true);
49+ e.value = value;
50+ document.dispatchEvent(e);
51+ }
52+ function recv(name, handler) {
53+ document.addEventListener(name + '_' + elm.id, handler);
54+ }
4855
49− // evaluate the given module and extract its 'main' value.
50− signalGraph = module(elm).main;
56+ // If graphics are not enabled, escape early, skip over setting up DOM stuff.
57+ if (baseNode === null) return { send : send, recv : recv };
5158
52− // make sure the signal graph is actually a signal, extract the visual model,
53− // and filter out any unused inputs.
54− var Signal = Elm.Signal(elm);
55− if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
56− visualModel = signalGraph.value;
57− inputs = ElmRuntime.filterDeadInputs(inputs);
5859
59− // Add the visualModel to the DOM
60− baseNode.appendChild(Render.render(visualModel));
60+ var Render = ElmRuntime.use(ElmRuntime.Render.Element);
6161
62− if ('Window' in elm) {
62+ // evaluate the given module and extract its 'main' value.
63+ signalGraph = module(elm).main;
64+
65+ // make sure the signal graph is actually a signal, extract the visual model,
66+ // and filter out any unused inputs.
67+ var Signal = Elm.Signal(elm);
68+ if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
69+ visualModel = signalGraph.value;
70+ inputs = ElmRuntime.filterDeadInputs(inputs);
71+
72+ // Add the visualModel to the DOM
73+ baseNode.appendChild(Render.render(visualModel));
74+
75+ if ('Window' in elm) {
6376 var w = baseNode.clientWidth;
6477 if (w !== elm.Window.dimensions.value._0) {
6578 notify(elm.Window.dimensions.id,
6679 Elm.Native.Utils(elm).Tuple2(w, baseNode.clientHeight));
6780 }
68− }
81+ }
6982
70− // set up updates so that the DOM is adjusted as necessary.
71− var update = Render.update;
72− function domUpdate(value) {
83+ // set up updates so that the DOM is adjusted as necessary.
84+ var update = Render.update;
85+ function domUpdate(value) {
7386 update(baseNode.firstChild, visualModel, value);
7487 visualModel = value;
7588 return value;
76− }
77−
78− signalGraph = A2(Signal.lift, domUpdate, signalGraph);
79− }
80−
81− function send(name, value) {
82− var e = document.createEvent('Event');
83− e.initEvent(name + '_' + elm.id, true, true);
84− e.value = value;
85− document.dispatchEvent(e);
8689 }
87− function recv(name, handler) {
88− document.addEventListener(name + '_' + elm.id, handler);
89− }
90−
90+
91+ signalGraph = A2(Signal.lift, domUpdate, signalGraph);
92+
9193 return { send : send, recv : recv };
9294};
runtime/Render/Collage.js+11−5
@@ -10,6 +10,7 @@ var newElement = Utils.newElement, addTo = Utils.addTo,
1010
1111function trace(ctx, path) {
1212 var points = fromList(path);
13+ console.log(points);
1314 var i = points.length - 1;
1415 if (i <= 0) return;
1516 ctx.moveTo(points[i]._0, points[i]._1);
@@ -88,6 +89,7 @@ function gradient(ctx, grad) {
8889}
8990
9091function drawShape(redo, ctx, style, path) {
92+ console.log(style, path);
9193 trace(ctx, path);
9294 var sty = style.ctor;
9395 ctx.fillStyle =
@@ -110,6 +112,7 @@ function renderForm(redo,ctx,form) {
110112 ctx.transform(m[0], m[3], m[1], m[4], m[2], m[5]);
111113 ctx.beginPath();
112114 var f = form.form;
115+ console.log(f);
113116 switch(f.ctor) {
114117 case 'FPath' : drawLine(ctx, f._0, f._1); break;
115118 case 'FShape':
@@ -123,9 +126,10 @@ function renderForm(redo,ctx,form) {
123126}
124127
125128function renderForms(redo, ctx, forms) {
126− forms = fromList(forms);
127− for (var i = forms.length; i--; ) {
128− renderForm(redo,ctx,forms[i]);
129+ var fs = fromList(forms);
130+ console.log(fs);
131+ for (var i = fs.length; i--; ) {
132+ renderForm(redo,ctx,fs[i]);
129133 }
130134}
131135
@@ -138,10 +142,11 @@ function collageForms(w,h,forms) {
138142 canvas.style.display = "block";
139143 canvas.width = w;
140144 canvas.height = h;
145+ function redo() { renderForms(this,ctx,forms); }
141146 if (canvas.getContext) {
142147 var ctx = canvas.getContext('2d');
143− function redo() { renderForms(this,ctx,w,h,forms); }
144− renderForms(redo,ctx,w,h,forms);
148+ console.log(forms);
149+ renderForms(redo,ctx,forms);
145150 return canvas;
146151 }
147152 canvas.innerHTML = "Your browser does not support canvas.";
@@ -162,6 +167,7 @@ function collageElement(w, h, m, elem) {
162167}
163168
164169function render(model) {
170+ console.log(model);
165171 return collageForms(model.w, model.h, model.forms);
166172}
167173