blob: 60d6981b0ab4bd1232e0e1e96775a2811e14f172 [file] [log] [blame]
Kevin Lubicke1b36fe2018-08-02 11:30:33 -04001<!DOCTYPE html>
2<title>PathKit (Skia + WASM)</title>
3<meta charset="utf-8" />
4<meta http-equiv="X-UA-Compatible" content="IE=edge">
5<meta name="viewport" content="width=device-width, initial-scale=1.0">
6
7<style>
8 svg, canvas {
9 border: 1px dashed #AAA;
10 }
11
12 canvas {
13 width: 200px;
14 height: 200px;
15 }
16
Kevin Lubick641bf872018-08-06 14:49:39 -040017 canvas.big {
18 width: 300px;
19 height: 300px;
20 }
21
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040022</style>
23
Kevin Lubick641bf872018-08-06 14:49:39 -040024<h2> Can output to an SVG Path, a Canvas, or a Path2D object </h2>
Kevin Lubick11194ab2018-08-17 13:52:56 -040025<svg id=svg1 xmlns='http://www.w3.org/2000/svg' width=200 height=200></svg>
Kevin Lubick641bf872018-08-06 14:49:39 -040026<canvas id=canvas1></canvas>
27<canvas id=canvas2></canvas>
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040028
Kevin Lubick641bf872018-08-06 14:49:39 -040029<h2> Interact with NewPath() just like a Path2D Object </h2>
30<canvas class=big id=canvas3></canvas>
31<canvas class=big id=canvas4></canvas>
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040032
Kevin Lubick97d6d982018-08-10 15:53:16 -040033<h2> Has various Path Effects </h2>
34<canvas class=big id=canvas5></canvas>
35<canvas class=big id=canvas6></canvas>
36<canvas class=big id=canvas7></canvas>
37<canvas class=big id=canvas8></canvas>
38<canvas class=big id=canvas9></canvas>
39<canvas class=big id=canvas10></canvas>
Kevin Lubick11194ab2018-08-17 13:52:56 -040040<canvas class=big id=canvasTransform></canvas>
41
42<h2> Supports fill-rules of nonzero and evenodd </h2>
43<svg id=svg2 xmlns='http://www.w3.org/2000/svg' width=200 height=200></svg>
44<svg id=svg3 xmlns='http://www.w3.org/2000/svg' width=200 height=200></svg>
Kevin Lubick97d6d982018-08-10 15:53:16 -040045
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040046<script type="text/javascript" src="/node_modules/experimental-pathkit-wasm/bin/pathkit.js"></script>
47
48<script type="text/javascript" charset="utf-8">
49
50 PathKitInit({
51 locateFile: (file) => '/node_modules/experimental-pathkit-wasm/bin/'+file,
52 }).then((PathKit) => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040053 window.PathKit = PathKit;
Kevin Lubick641bf872018-08-06 14:49:39 -040054 OutputsExample(PathKit);
55 Path2DExample(PathKit);
Kevin Lubick97d6d982018-08-10 15:53:16 -040056 PathEffectsExample(PathKit);
Kevin Lubick11194ab2018-08-17 13:52:56 -040057 MatrixTransformExample(PathKit);
58 FilledSVGExample(PathKit);
Kevin Lubick641bf872018-08-06 14:49:39 -040059 });
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040060
Kevin Lubick641bf872018-08-06 14:49:39 -040061 function setCanvasSize(ctx, width, height) {
62 ctx.canvas.width = width;
63 ctx.canvas.height = height;
64 }
65
66 function OutputsExample(PathKit) {
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040067 let firstPath = PathKit.FromSVGString('M11.99 2C6.47 2 2 6.48 2 12s4.47 10 9.99 10C17.52 22 22 17.52 22 12S17.52 2 11.99 2zM12 20c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8zm.5-13H11v6l5.25 3.15.75-1.23-4.5-2.67z');
68
69 let secondPath = PathKit.NewPath();
Kevin Lubick11194ab2018-08-17 13:52:56 -040070 // Acts somewhat like the Canvas API, except can be chained
71 secondPath.moveTo(1, 1)
72 .lineTo(20, 1)
73 .lineTo(10, 30)
74 .closePath();
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040075
Kevin Lubick11194ab2018-08-17 13:52:56 -040076 // Join the two paths together (mutating firstPath in the process)
77 firstPath.op(secondPath, PathKit.PathOp.INTERSECT);
78
79 let simpleStr = firstPath.toSVGString();
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040080
81 let newSVG = document.createElementNS('http://www.w3.org/2000/svg', 'path');
82 newSVG.setAttribute('stroke', 'rgb(0,0,200)');
83 newSVG.setAttribute('fill', 'white');
Kevin Lubick641bf872018-08-06 14:49:39 -040084 newSVG.setAttribute('transform', 'scale(8,8)');
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040085 newSVG.setAttribute('d', simpleStr);
Kevin Lubick11194ab2018-08-17 13:52:56 -040086 document.getElementById('svg1').appendChild(newSVG);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040087
88 // Draw directly to Canvas
89 let ctx = document.getElementById('canvas1').getContext('2d');
Kevin Lubick641bf872018-08-06 14:49:39 -040090 setCanvasSize(ctx, 200, 200);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040091 ctx.strokeStyle = 'green';
92 ctx.fillStyle = 'white';
Kevin Lubick641bf872018-08-06 14:49:39 -040093 ctx.scale(8, 8);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040094 ctx.beginPath();
Kevin Lubick11194ab2018-08-17 13:52:56 -040095 firstPath.toCanvas(ctx);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -040096 ctx.stroke();
97
98 // create Path2D object and use it in a Canvas.
Kevin Lubick11194ab2018-08-17 13:52:56 -040099 let path2D = firstPath.toPath2D();
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400100 ctx = document.getElementById('canvas2').getContext('2d');
Kevin Lubick641bf872018-08-06 14:49:39 -0400101 setCanvasSize(ctx, 200, 200);
102 ctx.canvas.width = 200
103 ctx.scale(8, 8);
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400104 ctx.fillStyle = 'purple';
105 ctx.strokeStyle = 'orange';
106 ctx.fill(path2D);
107 ctx.stroke(path2D);
108
109 // clean up WASM memory
110 // See http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html?highlight=memory#memory-management
111 firstPath.delete();
112 secondPath.delete();
Kevin Lubick641bf872018-08-06 14:49:39 -0400113 }
114
115 function Path2DExample(PathKit) {
116 let objs = [new Path2D(), PathKit.NewPath(), new Path2D(), PathKit.NewPath()];
117 let canvases = [
Kevin Lubick11194ab2018-08-17 13:52:56 -0400118 document.getElementById('canvas3').getContext('2d'),
119 document.getElementById('canvas4').getContext('2d')
Kevin Lubick641bf872018-08-06 14:49:39 -0400120 ];
121
122 for (i = 0; i <= 1; i++) {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400123 let path = objs[i];
Kevin Lubick641bf872018-08-06 14:49:39 -0400124
Kevin Lubick11194ab2018-08-17 13:52:56 -0400125 path.moveTo(20, 5);
126 path.lineTo(30, 20);
127 path.lineTo(40, 10);
128 path.lineTo(50, 20);
129 path.lineTo(60, 0);
130 path.lineTo(20, 5);
Kevin Lubick641bf872018-08-06 14:49:39 -0400131
Kevin Lubick11194ab2018-08-17 13:52:56 -0400132 path.moveTo(20, 80);
133 path.bezierCurveTo(90, 10, 160, 150, 190, 10);
Kevin Lubick641bf872018-08-06 14:49:39 -0400134
Kevin Lubick11194ab2018-08-17 13:52:56 -0400135 path.moveTo(36, 148);
136 path.quadraticCurveTo(66, 188, 120, 136);
137 path.lineTo(36, 148);
Kevin Lubick641bf872018-08-06 14:49:39 -0400138
Kevin Lubick11194ab2018-08-17 13:52:56 -0400139 path.rect(5, 170, 20, 20);
Kevin Lubick641bf872018-08-06 14:49:39 -0400140
Kevin Lubick11194ab2018-08-17 13:52:56 -0400141 path.moveTo(150, 180);
142 path.arcTo(150, 100, 50, 200, 20);
143 path.lineTo(160, 160);
Kevin Lubick641bf872018-08-06 14:49:39 -0400144
Kevin Lubick11194ab2018-08-17 13:52:56 -0400145 path.moveTo(20, 120);
146 path.arc(20, 120, 18, 0, 1.75 * Math.PI);
147 path.lineTo(20, 120);
Kevin Lubick641bf872018-08-06 14:49:39 -0400148
Kevin Lubick11194ab2018-08-17 13:52:56 -0400149 let secondPath = objs[i+2];
150 secondPath.ellipse(130, 25, 30, 10, -1*Math.PI/8, Math.PI/6, 1.5*Math.PI, false);
Kevin Lubick641bf872018-08-06 14:49:39 -0400151
Kevin Lubick11194ab2018-08-17 13:52:56 -0400152 path.addPath(secondPath);
Kevin Lubick641bf872018-08-06 14:49:39 -0400153
Kevin Lubick11194ab2018-08-17 13:52:56 -0400154 let m = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix();
155 m.a = 1; m.b = 0;
156 m.c = 0; m.d = 1;
157 m.e = 0; m.f = 20.5;
Kevin Lubick641bf872018-08-06 14:49:39 -0400158
Kevin Lubick11194ab2018-08-17 13:52:56 -0400159 path.addPath(secondPath, m);
160 // With PathKit, one can also just provide the 6 params as floats, to avoid
161 // the overhead of making an SVGMatrix
162 // path.addPath(secondPath, 1, 0, 0, 1, 0, 20.5);
Kevin Lubick641bf872018-08-06 14:49:39 -0400163
Kevin Lubick11194ab2018-08-17 13:52:56 -0400164 canvasCtx = canvases[i];
165 canvasCtx.fillStyle = 'blue';
166 setCanvasSize(canvasCtx, 300, 300);
167 canvasCtx.scale(1.5, 1.5);
168 if (path.toPath2D) {
169 canvasCtx.stroke(path.toPath2D());
170 } else {
171 canvasCtx.stroke(path);
172 }
Kevin Lubick641bf872018-08-06 14:49:39 -0400173 }
174
175
176 objs[1].delete();
177 }
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400178
Kevin Lubick97d6d982018-08-10 15:53:16 -0400179 // see https://fiddle.skia.org/c/@discrete_path
180 function drawStar(path) {
181 let R = 115.2, C = 128.0;
182 path.moveTo(C + R + 22, C);
183 for (let i = 1; i < 8; i++) {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400184 let a = 2.6927937 * i;
185 path.lineTo(C + R * Math.cos(a) + 22, C + R * Math.sin(a));
Kevin Lubick97d6d982018-08-10 15:53:16 -0400186 }
187 path.closePath();
188 return path;
189 }
190
191 function PathEffectsExample(PathKit) {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400192 let effects = [
193 // no-op
194 (path) => path,
195 // dash
196 (path) => path.dash(10, 3, 0),
197 // trim (takes optional 3rd param for returning the trimmed part
198 // or the complement)
199 (path) => path.trim(0.25, 0.8, false),
200 // simplify
201 (path) => path.simplify(),
202 // stroke
203 (path) => path.stroke({
204 width: 15,
205 join: PathKit.StrokeJoin.BEVEL,
206 cap: PathKit.StrokeCap.BUTT,
207 miter_limit: 1,
208 }),
209 // "offset effect", that is, making a border around the shape.
210 (path) => {
211 let orig = path.copy();
212 path.stroke({
213 width: 10,
214 join: PathKit.StrokeJoin.ROUND,
215 cap: PathKit.StrokeCap.SQUARE,
216 })
217 .op(orig, PathKit.PathOp.DIFFERENCE);
218 orig.delete();
219 }
Kevin Lubick97d6d982018-08-10 15:53:16 -0400220 ];
221
222 let names = ["(plain)", "Dash", "Trim", "Simplify", "Stroke", "Offset"];
223
Kevin Lubick11194ab2018-08-17 13:52:56 -0400224 for (let i = 0; i < effects.length; i++) {
225 let path = PathKit.NewPath();
226 drawStar(path);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400227
Kevin Lubick11194ab2018-08-17 13:52:56 -0400228 // The transforms apply directly to the path.
229 effects[i](path);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400230
Kevin Lubick11194ab2018-08-17 13:52:56 -0400231 let ctx = document.getElementById(`canvas${i+5}`).getContext('2d');
232 setCanvasSize(ctx, 300, 300);
233 ctx.strokeStyle = '#3c597a';
234 ctx.fillStyle = '#3c597a';
235 if (i === 4 || i === 5) {
236 ctx.fill(path.toPath2D(), path.getFillTypeString());
237 } else {
238 ctx.stroke(path.toPath2D());
239 }
Kevin Lubick97d6d982018-08-10 15:53:16 -0400240
Kevin Lubick11194ab2018-08-17 13:52:56 -0400241 ctx.font = '42px monospace';
Kevin Lubick97d6d982018-08-10 15:53:16 -0400242
Kevin Lubick11194ab2018-08-17 13:52:56 -0400243 let x = 150-ctx.measureText(names[i]).width/2;
244 ctx.strokeText(names[i], x, 290);
Kevin Lubick97d6d982018-08-10 15:53:16 -0400245
Kevin Lubick11194ab2018-08-17 13:52:56 -0400246 path.delete();
Kevin Lubick97d6d982018-08-10 15:53:16 -0400247 }
Kevin Lubick11194ab2018-08-17 13:52:56 -0400248 }
Kevin Lubick97d6d982018-08-10 15:53:16 -0400249
Kevin Lubick11194ab2018-08-17 13:52:56 -0400250 function MatrixTransformExample(PathKit) {
251 // Creates an animated star that twists and moves.
252 let ctx = document.getElementById('canvasTransform').getContext('2d');
253 setCanvasSize(ctx, 300, 300);
254 ctx.strokeStyle = '#3c597a';
255
256 let path = drawStar(PathKit.NewPath());
257 // TODO(kjlubick): Perhaps expose some matrix helper functions to allow
258 // clients to build their own matrices like this?
259 // These matrices represent a 2 degree rotation and a 1% scale factor.
260 let scaleUp = [1.0094, -0.0352, 3.1041,
261 0.0352, 1.0094, -6.4885,
262 0 , 0 , 1];
263
264 let scaleDown = [ 0.9895, 0.0346, -2.8473,
265 -0.0346, 0.9895, 6.5276,
266 0 , 0 , 1];
267
268 let i = 0;
269 function frame(){
270 i++;
271 if (Math.round(i/100) % 2) {
272 path.transform(scaleDown);
273 } else {
274 path.transform(scaleUp);
275 }
276
277 ctx.clearRect(0, 0, 300, 300);
278 ctx.stroke(path.toPath2D());
279
280 ctx.font = '42px monospace';
281 let x = 150-ctx.measureText('Transform').width/2;
282 ctx.strokeText('Transform', x, 290);
283
284 window.requestAnimationFrame(frame);
285 }
286 window.requestAnimationFrame(frame);
287 }
288
289 function FilledSVGExample(PathKit) {
290 let innerRect = PathKit.NewPath();
291 innerRect.rect(80, 100, 40, 40);
292
293 let outerRect = PathKit.NewPath();
294 outerRect.rect(50, 10, 100, 100)
295 .op(innerRect, PathKit.PathOp.XOR);
296
297 let str = outerRect.toSVGString();
298
299 let diffSVG = document.createElementNS('http://www.w3.org/2000/svg', 'path');
300 diffSVG.setAttribute('stroke', 'red');
301 diffSVG.setAttribute('fill', 'black');
302 // force fill-rule to nonzero to demonstrate difference
303 diffSVG.setAttribute('fill-rule', 'nonzero');
304 diffSVG.setAttribute('d', str);
305 document.getElementById('svg2').appendChild(diffSVG);
306
307 let unionSVG = document.createElementNS('http://www.w3.org/2000/svg', 'path');
308 unionSVG.setAttribute('stroke', 'red');
309 unionSVG.setAttribute('fill', 'black');
310 // ask what the path thinks fill-rule should be ('evenodd')
311 // SVG and Canvas both use the same keys ('nonzero' and 'evenodd') and both
312 // default to 'nonzero', so one call supports both.
313 unionSVG.setAttribute('fill-rule', outerRect.getFillTypeString());
314 unionSVG.setAttribute('d', str);
315 document.getElementById('svg3').appendChild(unionSVG);
316
317 outerRect.delete();
318 innerRect.delete();
Kevin Lubick97d6d982018-08-10 15:53:16 -0400319 }
320
Kevin Lubicke1b36fe2018-08-02 11:30:33 -0400321</script>