2 <title>CanvasKit (Skia via Web Assembly)</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">
9 border: 1px dashed #AAA;
18 <h2>Drop in replacement for HTML Canvas (e.g. node.js)</h2>
19 <img id=api1 width=300 height=300>
20 <canvas id=api1_c width=300 height=300></canvas>
21 <img id=api2 width=300 height=300>
22 <canvas id=api2_c width=300 height=300></canvas>
23 <img id=api3 width=300 height=300>
24 <canvas id=api3_c width=300 height=300></canvas>
25 <img id=api4 width=300 height=300>
26 <canvas id=api4_c width=300 height=300></canvas>
27 <img id=api5 width=300 height=300>
28 <canvas id=api5_c width=300 height=300></canvas>
29 <img id=api6 width=300 height=300>
30 <canvas id=api6_c width=300 height=300></canvas>
31 <img id=api7 width=300 height=300>
32 <canvas id=api7_c width=300 height=300></canvas>
33 <img id=api8 width=300 height=300>
34 <canvas id=api8_c width=300 height=300></canvas>
36 <h2> CanvasKit expands the functionality of a stock HTML canvas</h2>
37 <canvas id=vertex1 width=300 height=300></canvas>
38 <canvas id=gradient1 width=300 height=300></canvas>
39 <canvas id=patheffect width=300 height=300></canvas>
40 <canvas id=paths width=200 height=200></canvas>
41 <canvas id=pathperson width=300 height=300></canvas>
42 <canvas id=ink width=300 height=300></canvas>
43 <canvas id=surfaces width=300 height=300></canvas>
44 <canvas id=atlas width=300 height=300></canvas>
45 <canvas id=decode width=300 height=300></canvas>
47 <h2> CanvasKit can allow for text measurement/placement (e.g. breaking, kerning)</h2>
48 <canvas id=textonpath width=300 height=300></canvas>
49 <canvas id=drawGlyphs width=300 height=300></canvas>
51 <h2> Interactive drawPatch</h2>
52 <canvas id=interdrawpatch width=512 height=512></canvas>
54 <script type="text/javascript" src="/build/canvaskit.js"></script>
56 <script type="text/javascript" charset="utf-8">
59 var cdn = 'https://storage.googleapis.com/skia-cdn/misc/';
61 const ckLoaded = CanvasKitInit({locateFile: (file) => '/build/'+file});
63 const loadRoboto = fetch(cdn + 'Roboto-Regular.ttf').then((response) => response.arrayBuffer());
64 const loadNotoSerif = fetch(cdn + 'NotoSerif-Regular.ttf').then((response) => response.arrayBuffer());
65 const loadTestImage = fetch(cdn + 'test.png').then((response) => response.arrayBuffer());
67 // Examples which only require canvaskit
68 ckLoaded.then((CK) => {
70 PathExample(CanvasKit);
71 InkExample(CanvasKit);
72 PathPersonExample(CanvasKit);
73 VertexAPI1(CanvasKit);
74 GradiantAPI1(CanvasKit);
75 TextOnPathAPI1(CanvasKit);
76 DrawGlyphsAPI1(CanvasKit);
77 SurfaceAPI1(CanvasKit);
78 if (CanvasKit.MakeCanvas){
79 CanvasAPI1(CanvasKit);
80 CanvasAPI2(CanvasKit);
81 CanvasAPI3(CanvasKit);
82 CanvasAPI4(CanvasKit);
83 CanvasAPI5(CanvasKit);
84 CanvasAPI6(CanvasKit);
85 CanvasAPI7(CanvasKit);
86 CanvasAPI8(CanvasKit);
88 console.log("Skipping CanvasAPI1 because it's not compiled in");
90 InteractivePatch(CanvasKit);
93 // Examples requiring external resources
94 Promise.all([ckLoaded, loadRoboto]).then((results) => {DrawingExample(...results)});
95 Promise.all([ckLoaded, loadTestImage]).then((results) => {AtlasAPI1(...results)});
96 Promise.all([ckLoaded, loadTestImage]).then((results) => {DecodeAPI(...results)});
98 function DrawingExample(CanvasKit, robotoData) {
99 if (!robotoData || !CanvasKit) {
102 const surface = CanvasKit.MakeCanvasSurface('patheffect');
104 console.error('Could not make surface');
108 const paint = new CanvasKit.Paint();
109 const roboto = CanvasKit.Typeface.MakeFreeTypeFaceFromData(robotoData);
111 const textPaint = new CanvasKit.Paint();
112 textPaint.setColor(CanvasKit.RED);
113 textPaint.setAntiAlias(true);
115 const textFont = new CanvasKit.Font(roboto, 30);
122 function drawFrame(canvas) {
123 const path = starPath(CanvasKit, X, Y);
124 // Some animations see performance improvements by marking their
125 // paths as volatile.
126 path.setIsVolatile(true);
127 const dpe = CanvasKit.PathEffect.MakeDash([15, 5, 5, 10], i/5);
130 paint.setPathEffect(dpe);
131 paint.setStyle(CanvasKit.PaintStyle.Stroke);
132 paint.setStrokeWidth(5.0 + -3 * Math.cos(i/30));
133 paint.setAntiAlias(true);
134 paint.setColor(CanvasKit.Color(66, 129, 164, 1.0));
136 canvas.clear(CanvasKit.TRANSPARENT);
138 canvas.drawPath(path, paint);
139 canvas.drawText('Try Clicking!', 10, 280, textPaint, textFont);
143 surface.requestAnimationFrame(drawFrame);
145 surface.requestAnimationFrame(drawFrame);
147 // Make animation interactive
148 let interact = (e) => {
155 document.getElementById('patheffect').addEventListener('pointermove', interact);
156 document.getElementById('patheffect').addEventListener('pointerdown', interact);
157 preventScrolling(document.getElementById('patheffect'));
158 // A client would need to delete this if it didn't go on for ever.
160 // textPaint.delete();
161 // textFont.delete();
164 function InteractivePatch(CanvasKit) {
165 const ELEM = 'interdrawpatch';
166 const surface = CanvasKit.MakeCanvasSurface(ELEM);
168 console.error('Could not make surface');
172 let live_corner, live_index;
174 const paint = new CanvasKit.Paint();
175 const pts_paint = new CanvasKit.Paint();
176 pts_paint.setStyle(CanvasKit.PaintStyle.Stroke);
177 pts_paint.setStrokeWidth(9);
178 pts_paint.setStrokeCap(CanvasKit.StrokeCap.Round);
180 const line_paint = new CanvasKit.Paint();
181 line_paint.setStyle(CanvasKit.PaintStyle.Stroke);
182 line_paint.setStrokeWidth(2);
184 const colors = [CanvasKit.RED, CanvasKit.BLUE, CanvasKit.YELLOW, CanvasKit.CYAN];
187 [ 10,170, 10, 10, 170, 10], // prev_vector, point, next_vector
188 [340, 10, 500, 10, 500,170],
189 [500,340, 500,500, 340,500],
190 [170,500, 10,500, 10,340],
193 function get_corner(corner, index) {
194 return [corner[index*2+0], corner[index*2+1]];
197 function push_xy(array, xy) {
198 array.push(xy[0], xy[1]);
201 function patch_to_cubics(patch) {
203 push_xy(array, get_corner(patch[0],1));
204 push_xy(array, get_corner(patch[0],2));
205 for (let i = 1; i < 4; ++i) {
206 push_xy(array, get_corner(patch[i],0));
207 push_xy(array, get_corner(patch[i],1));
208 push_xy(array, get_corner(patch[i],2));
210 push_xy(array, get_corner(patch[0],0));
215 function drawFrame(canvas) {
216 const cubics = patch_to_cubics(patch);
218 canvas.drawColor(CanvasKit.WHITE);
219 canvas.drawPatch(cubics, colors, null, CanvasKit.BlendMode.Dst, paint);
221 canvas.drawPoints(CanvasKit.PointMode.Polygon, live_corner, line_paint);
223 canvas.drawPoints(CanvasKit.PointMode.Points, cubics, pts_paint);
225 surface.requestAnimationFrame(drawFrame);
228 surface.requestAnimationFrame(drawFrame);
230 function length2(x, y) {
233 function hit_test(x,y, x1,y1) {
234 return length2(x-x1, y-y1) <= 10*10;
236 function pointer_up(e) {
241 function pointer_down(e) {
245 for (let i = 0; i < 6; i += 2) {
246 if (hit_test(p[i], p[i+1], e.offsetX, e.offsetY)) {
254 function pointer_move(e) {
255 if (e.pressure && live_corner) {
256 if (live_index == 2) {
258 const dx = e.offsetX - live_corner[2];
259 const dy = e.offsetY - live_corner[3];
260 for (let i = 0; i < 3; ++i) {
261 live_corner[i*2+0] += dx;
262 live_corner[i*2+1] += dy;
266 live_corner[live_index+0] = e.offsetX;
267 live_corner[live_index+1] = e.offsetY;
271 document.getElementById(ELEM).addEventListener('pointermove', pointer_move);
272 document.getElementById(ELEM).addEventListener('pointerdown', pointer_down);
273 document.getElementById(ELEM).addEventListener('pointerup', pointer_up);
274 preventScrolling(document.getElementById(ELEM));
277 function PathPersonExample(CanvasKit) {
278 const surface = CanvasKit.MakeSWCanvasSurface('pathperson');
280 console.error('Could not make surface');
284 function drawFrame(canvas) {
285 const paint = new CanvasKit.Paint();
286 paint.setStrokeWidth(1.0);
287 paint.setAntiAlias(true);
288 paint.setColor(CanvasKit.Color(0, 0, 0, 1.0));
289 paint.setStyle(CanvasKit.PaintStyle.Stroke);
291 const path = new CanvasKit.Path();
293 path.lineTo(100, 10);
295 path.lineTo(10, 200);
296 path.moveTo(10, 100);
297 path.lineTo(100,100);
298 path.moveTo(10, 200);
299 path.lineTo(100, 200);
301 canvas.drawPath(path, paint);
305 // Intentionally just draw frame once
306 surface.drawOnce(drawFrame);
309 function PathExample(CanvasKit) {
310 const surface = CanvasKit.MakeSWCanvasSurface('paths');
312 console.error('Could not make surface');
316 function drawFrame(canvas) {
317 const paint = new CanvasKit.Paint();
318 paint.setStrokeWidth(1.0);
319 paint.setAntiAlias(true);
320 paint.setColor(CanvasKit.Color(0, 0, 0, 1.0));
321 paint.setStyle(CanvasKit.PaintStyle.Stroke);
323 const path = new CanvasKit.Path();
332 path.cubicTo(90, 10, 160, 150, 190, 10);
334 path.moveTo(36, 148);
335 path.quadTo(66, 188, 120, 136);
336 path.lineTo(36, 148);
338 path.moveTo(150, 180);
339 path.arcToTangent(150, 100, 50, 200, 20);
340 path.lineTo(160, 160);
342 path.moveTo(20, 120);
343 path.lineTo(20, 120);
345 canvas.drawPath(path, paint);
347 const rrect = CanvasKit.RRectXY([100, 10, 140, 62], 10, 4);
349 const rrectPath = new CanvasKit.Path().addRRect(rrect, true);
351 canvas.drawPath(rrectPath, paint);
357 // Intentionally just draw frame once
358 surface.drawOnce(drawFrame);
361 function preventScrolling(canvas) {
362 canvas.addEventListener('touchmove', (e) => {
363 // Prevents touch events in the canvas from scrolling the canvas.
369 function InkExample(CanvasKit) {
370 const surface = CanvasKit.MakeCanvasSurface('ink');
372 console.error('Could not make surface');
376 let paint = new CanvasKit.Paint();
377 paint.setAntiAlias(true);
378 paint.setColor(CanvasKit.Color(0, 0, 0, 1.0));
379 paint.setStyle(CanvasKit.PaintStyle.Stroke);
380 paint.setStrokeWidth(4.0);
381 paint.setPathEffect(CanvasKit.PathEffect.MakeCorner(50));
384 let path = new CanvasKit.Path();
388 path.moveTo(100, 80);
389 path.lineTo(100, 15);
390 path.lineTo(130, 95);
391 path.lineTo(130, 30);
393 path.moveTo(150, 30);
394 path.lineTo(150, 80);
395 path.moveTo(170, 30);
396 path.lineTo(150, 55);
397 path.lineTo(170, 80);
400 let paints = [paint];
402 function drawFrame(canvas) {
403 canvas.clear(CanvasKit.Color(255, 255, 255, 1.0));
405 for (let i = 0; i < paints.length && i < paths.length; i++) {
406 canvas.drawPath(paths[i], paints[i]);
409 surface.requestAnimationFrame(drawFrame);
413 let interact = (e) => {
415 if (type === 'lostpointercapture' || type === 'pointerup' || !e.pressure ) {
420 path.lineTo(e.offsetX, e.offsetY);
422 paint = paint.copy();
423 paint.setColor(CanvasKit.Color(Math.random() * 255, Math.random() * 255, Math.random() * 255, Math.random() + .2));
425 path = new CanvasKit.Path();
427 path.moveTo(e.offsetX, e.offsetY);
431 document.getElementById('ink').addEventListener('pointermove', interact);
432 document.getElementById('ink').addEventListener('pointerdown', interact);
433 document.getElementById('ink').addEventListener('lostpointercapture', interact);
434 document.getElementById('ink').addEventListener('pointerup', interact);
435 preventScrolling(document.getElementById('ink'));
436 surface.requestAnimationFrame(drawFrame);
439 function starPath(CanvasKit, X=128, Y=128, R=116) {
440 let p = new CanvasKit.Path();
442 for (let i = 1; i < 8; i++) {
443 let a = 2.6927937 * i;
444 p.lineTo(X + R * Math.cos(a), Y + R * Math.sin(a));
449 function CanvasAPI1(CanvasKit) {
450 let skcanvas = CanvasKit.MakeCanvas(300, 300);
451 let realCanvas = document.getElementById('api1_c');
453 let skPromise = fetch(cdn + 'test.png')
454 // if clients want to use a Blob, they are responsible
455 // for reading it themselves.
456 .then((response) => response.arrayBuffer())
458 skcanvas._img = skcanvas.decodeImage(buffer);
460 let realPromise = fetch(cdn + 'test.png')
461 .then((response) => response.blob())
462 .then((blob) => createImageBitmap(blob))
464 realCanvas._img = bitmap;
467 let realFontLoaded = new FontFace('Bungee', 'url(/tests/assets/Bungee-Regular.ttf)', {
471 }).load().then((font) => {
472 document.fonts.add(font);
475 let skFontLoaded = fetch('/tests/assets/Bungee-Regular.ttf').then(
476 (response) => response.arrayBuffer()).then(
478 // loadFont is synchronous
479 skcanvas.loadFont(buffer, {
486 Promise.all([realPromise, skPromise, realFontLoaded, skFontLoaded]).then(() => {
487 for (let canvas of [skcanvas, realCanvas]) {
488 let ctx = canvas.getContext('2d');
489 ctx.fillStyle = '#EEE';
490 ctx.fillRect(0, 0, 300, 300);
491 ctx.fillStyle = 'black';
492 ctx.font = '26px Bungee';
494 let text = ctx.measureText('Awesome');
495 ctx.fillText('Awesome ', 25, 100);
496 ctx.strokeText('Groovy!', 35 + text.width, 100);
498 // Draw line under Awesome
499 ctx.strokeStyle = 'rgba(125,0,0,0.5)';
503 ctx.lineTo(200, 105);
506 // squished vertically
507 ctx.globalAlpha = 0.7;
508 ctx.imageSmoothingQuality = 'medium';
509 ctx.drawImage(canvas._img, 150, 150, 150, 100);
511 ctx.imageSmoothingEnabled = false;
512 ctx.drawImage(canvas._img, 100, 150, 400, 350, 10, 200, 150, 100);
514 let idata = ctx.getImageData(80, 220, 40, 45);
515 ctx.putImageData(idata, 250, 10);
516 ctx.putImageData(idata, 200, 10, 20, 10, 20, 30);
517 ctx.resetTransform();
518 ctx.strokeStyle = 'black';
520 ctx.strokeRect(200, 10, 40, 45);
522 idata = ctx.createImageData(10, 20);
523 ctx.putImageData(idata, 10, 10);
526 document.getElementById('api1').src = skcanvas.toDataURL();
532 function CanvasAPI2(CanvasKit) {
533 let skcanvas = CanvasKit.MakeCanvas(300, 300);
534 let realCanvas = document.getElementById('api2_c');
535 realCanvas.width = 300;
536 realCanvas.height = 300;
538 // svg data for a clock
539 skcanvas._path = skcanvas.makePath2D('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');
540 realCanvas._path = new Path2D('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');
542 for (let canvas of [skcanvas, realCanvas]) {
543 let ctx = canvas.getContext('2d');
553 ctx.bezierCurveTo(90, 10, 160, 150, 190, 10);
556 ctx.quadraticCurveTo(66, 188, 120, 136);
559 ctx.rect(5, 170, 20, 25);
561 ctx.moveTo(150, 180);
562 ctx.arcTo(150, 100, 50, 200, 20);
563 ctx.lineTo(160, 160);
566 ctx.arc(20, 120, 18, 0, 1.75 * Math.PI);
570 ctx.ellipse(130, 25, 30, 10, -1*Math.PI/8, Math.PI/6, 1.5*Math.PI);
576 ctx.stroke(canvas._path);
578 // Test edgecases and draw direction
580 ctx.arc(50, 100, 10, Math.PI, -Math.PI/2);
583 ctx.arc(75, 100, 10, Math.PI, -Math.PI/2, true);
586 ctx.arc(100, 100, 10, Math.PI, 100.1 * Math.PI, true);
589 ctx.arc(125, 100, 10, Math.PI, 100.1 * Math.PI, false);
592 ctx.ellipse(155, 100, 10, 15, Math.PI/8, 100.1 * Math.PI, Math.PI, true);
595 ctx.ellipse(180, 100, 10, 15, Math.PI/8, Math.PI, 100.1 * Math.PI, true);
598 document.getElementById('api2').src = skcanvas.toDataURL();
602 function CanvasAPI3(CanvasKit) {
603 let skcanvas = CanvasKit.MakeCanvas(300, 300);
604 let realCanvas = document.getElementById('api3_c');
605 realCanvas.width = 300;
606 realCanvas.height = 300;
608 for (let canvas of [skcanvas, realCanvas]) {
609 let ctx = canvas.getContext('2d');
610 ctx.rect(10, 10, 20, 20);
613 ctx.rect(30, 10, 20, 20);
614 ctx.resetTransform();
616 ctx.rotate(Math.PI / 3);
617 ctx.rect(50, 10, 20, 20);
618 ctx.resetTransform();
620 ctx.translate(30, -2);
621 ctx.rect(70, 10, 20, 20);
622 ctx.resetTransform();
624 ctx.translate(60, 0);
625 ctx.rotate(Math.PI / 6);
626 ctx.transform(1.5, 0, 0, 0.5, 0, 0); // effectively scale
627 ctx.rect(90, 10, 20, 20);
628 ctx.resetTransform();
631 ctx.setTransform(2, 0, -.5, 2.5, -40, 120);
632 ctx.rect(110, 10, 20, 20);
635 ctx.lineTo(220, 120);
638 ctx.font = '6pt Noto Mono';
639 ctx.fillText('This text should be huge', 10, 80);
640 ctx.resetTransform();
642 ctx.strokeStyle = 'black';
650 ctx.lineTo(280/3, 90/3);
652 ctx.strokeStyle = 'black';
657 document.getElementById('api3').src = skcanvas.toDataURL();
661 function CanvasAPI4(CanvasKit) {
662 let skcanvas = CanvasKit.MakeCanvas(300, 300);
663 let realCanvas = document.getElementById('api4_c');
664 realCanvas.width = 300;
665 realCanvas.height = 300;
667 for (let canvas of [skcanvas, realCanvas]) {
668 let ctx = canvas.getContext('2d');
670 ctx.strokeStyle = '#000';
671 ctx.fillStyle = '#CCC';
672 ctx.shadowColor = 'rebeccapurple';
674 ctx.shadowOffsetX = 3;
675 ctx.shadowOffsetY = -8;
676 ctx.rect(10, 10, 30, 30);
679 ctx.strokeStyle = '#C00';
680 ctx.fillStyle = '#00C';
682 ctx.shadowColor = 'transparent';
691 ctx.quadraticCurveTo(66, 188, 120, 136);
696 ctx.shadowColor = '#993366AA';
697 ctx.shadowOffsetX = 8;
699 ctx.setTransform(2, 0, -.5, 2.5, -40, 120);
700 ctx.rect(110, 10, 20, 20);
702 ctx.resetTransform();
703 ctx.lineTo(220, 120);
706 ctx.fillStyle = 'green';
707 ctx.font = '16pt Noto Mono';
708 ctx.fillText('This should be shadowed', 20, 80);
712 ctx.ellipse(10, 290, 30, 30, 0, 0, Math.PI * 2);
715 ctx.ellipse(10, 290, 30, 60, 0, 0, Math.PI * 2);
716 ctx.resetTransform();
719 ctx.ellipse(10, 290, 30, 90, 0, 0, Math.PI * 2);
722 document.getElementById('api4').src = skcanvas.toDataURL();
726 function CanvasAPI5(CanvasKit) {
727 let skcanvas = CanvasKit.MakeCanvas(600, 600);
728 let realCanvas = document.getElementById('api5_c');
729 realCanvas.width = 600;
730 realCanvas.height = 600;
732 for (let canvas of [skcanvas, realCanvas]) {
733 let ctx = canvas.getContext('2d');
735 ctx.translate(10, 10);
736 // Shouldn't impact the fillRect calls
737 ctx.setLineDash([5, 3]);
739 ctx.fillStyle = 'rgba(200, 0, 100, 0.81)';
740 ctx.fillRect(20, 30, 100, 100);
742 ctx.globalAlpha = 0.81;
743 ctx.fillStyle = 'rgba(200, 0, 100, 1.0)';
744 ctx.fillRect(120, 30, 100, 100);
745 // This shouldn't do anything
746 ctx.globalAlpha = 0.1;
748 ctx.fillStyle = 'rgba(200, 0, 100, 0.9)';
749 ctx.globalAlpha = 0.9;
750 // Intentional no-op to check ordering
751 ctx.clearRect(220, 30, 100, 100);
752 ctx.fillRect(220, 30, 100, 100);
754 ctx.fillRect(320, 30, 100, 100);
755 ctx.clearRect(330, 40, 80, 80);
757 ctx.strokeStyle = 'blue';
759 ctx.setLineDash([5, 3]);
760 ctx.strokeRect(20, 150, 100, 100);
761 ctx.setLineDash([50, 30]);
762 ctx.strokeRect(125, 150, 100, 100);
763 ctx.lineDashOffset = 25;
764 ctx.strokeRect(230, 150, 100, 100);
765 ctx.setLineDash([2, 5, 9]);
766 ctx.strokeRect(335, 150, 100, 100);
768 ctx.setLineDash([5, 2]);
769 ctx.moveTo(336, 400);
770 ctx.quadraticCurveTo(366, 488, 120, 450);
771 ctx.lineTo(300, 400);
774 ctx.font = '36pt Noto Mono';
775 ctx.strokeText('Dashed', 20, 350);
776 ctx.fillText('Not Dashed', 20, 400);
779 document.getElementById('api5').src = skcanvas.toDataURL();
783 function CanvasAPI6(CanvasKit) {
784 let skcanvas = CanvasKit.MakeCanvas(600, 600);
785 let realCanvas = document.getElementById('api6_c');
786 realCanvas.width = 600;
787 realCanvas.height = 600;
789 for (let canvas of [skcanvas, realCanvas]) {
790 let ctx = canvas.getContext('2d');
792 let rgradient = ctx.createRadialGradient(200, 300, 10, 100, 100, 300);
794 // Add three color stops
795 rgradient.addColorStop(0, 'red');
796 rgradient.addColorStop(0.7, 'white');
797 rgradient.addColorStop(1, 'blue');
799 ctx.fillStyle = rgradient;
800 ctx.globalAlpha = 0.7;
801 ctx.fillRect(0, 0, 600, 600);
802 ctx.globalAlpha = 0.95;
805 ctx.arc(300, 100, 90, 0, Math.PI*1.66);
807 ctx.strokeStyle = 'yellow';
813 let lgradient = ctx.createLinearGradient(200, 20, 420, 40);
815 // Add three color stops
816 lgradient.addColorStop(0, 'green');
817 lgradient.addColorStop(0.5, 'cyan');
818 lgradient.addColorStop(1, 'orange');
820 ctx.fillStyle = lgradient;
822 ctx.fillRect(200, 30, 200, 300);
825 ctx.fillRect(550, 550, 40, 40);
828 document.getElementById('api6').src = skcanvas.toDataURL();
832 function CanvasAPI7(CanvasKit) {
833 let skcanvas = CanvasKit.MakeCanvas(300, 300);
834 let realCanvas = document.getElementById('api7_c');
836 let skPromise = fetch(cdn + 'test.png')
837 // if clients want to use a Blob, they are responsible
838 // for reading it themselves.
839 .then((response) => response.arrayBuffer())
841 skcanvas._img = skcanvas.decodeImage(buffer);
843 let realPromise = fetch(cdn + 'test.png')
844 .then((response) => response.blob())
845 .then((blob) => createImageBitmap(blob))
847 realCanvas._img = bitmap;
851 Promise.all([realPromise, skPromise]).then(() => {
852 for (let canvas of [skcanvas, realCanvas]) {
853 let ctx = canvas.getContext('2d');
854 ctx.fillStyle = '#EEE';
855 ctx.fillRect(0, 0, 300, 300);
859 let pattern = ctx.createPattern(canvas._img, 'repeat');
860 ctx.fillStyle = pattern;
861 ctx.fillRect(0, 0, 1500, 750);
863 pattern = ctx.createPattern(canvas._img, 'repeat-x');
864 ctx.fillStyle = pattern;
865 ctx.fillRect(1500, 0, 3000, 750);
867 ctx.globalAlpha = 0.7;
868 pattern = ctx.createPattern(canvas._img, 'repeat-y');
869 ctx.fillStyle = pattern;
870 ctx.fillRect(0, 750, 1500, 1500);
871 ctx.strokeRect(0, 750, 1500, 1500);
873 pattern = ctx.createPattern(canvas._img, 'no-repeat');
874 ctx.fillStyle = pattern;
875 pattern.setTransform({a: 1, b: -.1, c:.1, d: 0.5, e: 1800, f:800});
876 ctx.fillRect(0, 0, 3000, 1500);
879 document.getElementById('api7').src = skcanvas.toDataURL();
884 function CanvasAPI8(CanvasKit) {
885 let skcanvas = CanvasKit.MakeCanvas(300, 300);
886 let realCanvas = document.getElementById('api8_c');
888 function drawPoint(ctx, x, y, color) {
889 ctx.fillStyle = color;
890 ctx.fillRect(x, y, 1, 1);
893 const OUT = 'orange';
896 const pts = [[3, 3], [4, 4], [5, 5], [10, 10], [8, 10], [6, 10],
897 [6.5, 9], [15, 10], [17, 10], [17, 11], [24, 24],
898 [25, 25], [26, 26], [27, 27]];
906 testFn: (ctx, x, y) => ctx.isPointInPath(x * SCALE, y * SCALE, 'nonzero'),
913 testFn: (ctx, x, y) => ctx.isPointInPath(x * SCALE, y * SCALE, 'evenodd'),
920 testFn: (ctx, x, y) => ctx.isPointInStroke(x * SCALE, y * SCALE),
927 testFn: (ctx, x, y) => ctx.isPointInStroke(x * SCALE, y * SCALE),
931 for (let canvas of [skcanvas, realCanvas]) {
932 let ctx = canvas.getContext('2d');
933 ctx.font = '11px Noto Mono';
934 // Draw some visual aids
935 ctx.fillText('path-nonzero', 30, 15);
936 ctx.fillText('path-evenodd', 150, 15);
937 ctx.fillText('stroke-1px-wide', 30, 130);
938 ctx.fillText('stroke-2px-wide', 150, 130);
939 ctx.fillText('purple is IN, orange is OUT', 10, 280);
941 // Scale up to make single pixels easier to see
942 ctx.scale(SCALE, SCALE);
943 for (let test of tests) {
945 let xOffset = test.xOffset;
946 let yOffset = test.yOffset;
948 ctx.fillStyle = '#AAA';
949 ctx.lineWidth = test.strokeWidth;
950 ctx.rect(5+xOffset, 5+yOffset, 20, 20);
951 ctx.arc(15+xOffset, 15+yOffset, 8, 0, Math.PI*2, false);
953 ctx.fill(test.fillType);
958 for (let pt of pts) {
962 // naively apply transform when querying because the points queried
964 if (test.testFn(ctx, x, y)) {
965 drawPoint(ctx, x, y, IN);
967 drawPoint(ctx, x, y, OUT);
973 document.getElementById('api8').src = skcanvas.toDataURL();
977 function VertexAPI1(CanvasKit) {
978 const surface = CanvasKit.MakeCanvasSurface('vertex1');
980 console.error('Could not make surface');
983 const canvas = surface.getCanvas();
984 let paint = new CanvasKit.Paint();
986 // See https://fiddle.skia.org/c/f48b22eaad1bb7adcc3faaa321754af6
987 // for original c++ version.
988 let points = [0, 0, 250, 0, 100, 100, 0, 250];
989 let colors = [CanvasKit.RED, CanvasKit.BLUE,
990 CanvasKit.YELLOW, CanvasKit.CYAN];
991 let vertices = CanvasKit.MakeVertices(CanvasKit.VertexMode.TriangleFan,
992 points, null, colors,
993 false /*isVolatile*/);
995 canvas.drawVertices(vertices, CanvasKit.BlendMode.Dst, paint);
999 // See https://fiddle.skia.org/c/e8bdae9bea3227758989028424fcac3d
1000 // for original c++ version.
1001 points = [300, 300, 50, 300, 200, 200, 300, 50 ];
1002 let texs = [ 0, 0, 0, 250, 250, 250, 250, 0 ];
1003 vertices = CanvasKit.MakeVertices(CanvasKit.VertexMode.TriangleFan,
1004 points, texs, colors);
1006 let shader = CanvasKit.Shader.MakeLinearGradient([0, 0], [250, 0],
1007 colors, null, CanvasKit.TileMode.Clamp);
1008 paint.setShader(shader);
1010 canvas.drawVertices(vertices, CanvasKit.BlendMode.Darken, paint);
1018 function GradiantAPI1(CanvasKit) {
1019 const surface = CanvasKit.MakeSWCanvasSurface('gradient1');
1021 console.error('Could not make surface');
1024 const canvas = surface.getCanvas();
1025 let paint = new CanvasKit.Paint();
1027 // See https://fiddle.skia.org/c/f48b22eaad1bb7adcc3faaa321754af6
1028 // for original c++ version.
1029 let colors = [CanvasKit.BLUE, CanvasKit.YELLOW, CanvasKit.RED];
1030 let pos = [0, .7, 1.0];
1031 let transform = [2, 0, 0,
1034 let shader = CanvasKit.Shader.MakeRadialGradient([150, 150], 130, colors,
1035 pos, CanvasKit.TileMode.Mirror, transform);
1037 paint.setShader(shader);
1038 const textFont = new CanvasKit.Font(null, 75);
1039 const textBlob = CanvasKit.TextBlob.MakeFromText('Radial', textFont);
1041 canvas.drawTextBlob(textBlob, 10, 200, paint);
1048 function TextOnPathAPI1(CanvasKit) {
1049 const surface = CanvasKit.MakeSWCanvasSurface('textonpath');
1051 console.error('Could not make surface');
1054 const canvas = surface.getCanvas();
1055 const paint = new CanvasKit.Paint();
1056 paint.setStyle(CanvasKit.PaintStyle.Stroke);
1057 paint.setAntiAlias(true);
1059 const font = new CanvasKit.Font(null, 24);
1060 const fontPaint = new CanvasKit.Paint();
1061 fontPaint.setStyle(CanvasKit.PaintStyle.Fill);
1062 fontPaint.setAntiAlias(true);
1064 const arc = new CanvasKit.Path();
1065 arc.arcToOval(CanvasKit.LTRBRect(20, 40, 280, 300), -160, 140, true);
1066 arc.lineTo(210, 140);
1067 arc.arcToOval(CanvasKit.LTRBRect(20, 0, 280, 260), 160, -140, true);
1069 const str = 'This téxt should follow the curve across contours...';
1070 const textBlob = CanvasKit.TextBlob.MakeOnPath(str, arc, font);
1072 canvas.drawPath(arc, paint);
1073 canvas.drawTextBlob(textBlob, 0, 0, fontPaint);
1084 function DrawGlyphsAPI1(CanvasKit) {
1085 const surface = CanvasKit.MakeSWCanvasSurface('drawGlyphs');
1087 console.error('Could not make surface');
1090 const canvas = surface.getCanvas();
1091 const paint = new CanvasKit.Paint();
1092 const font = new CanvasKit.Font(null, 16);
1093 paint.setAntiAlias(true);
1097 for (let i = 0; i < 256; ++i) {
1099 positions.push((i % 16) * 16);
1100 positions.push(Math.round(i/16) * 16);
1102 canvas.drawGlyphs(glyphs, positions, 16, 20, font, paint);
1110 function SurfaceAPI1(CanvasKit) {
1111 const surface = CanvasKit.MakeCanvasSurface('surfaces');
1113 console.error('Could not make surface');
1117 // create a subsurface as a temporary workspace.
1118 const subSurface = surface.makeSurface({
1121 alphaType: CanvasKit.AlphaType.Premul,
1122 colorType: CanvasKit.ColorType.RGBA_8888,
1123 colorSpace: CanvasKit.ColorSpace.SRGB,
1127 console.error('Could not make subsurface');
1131 // draw a small "scene"
1132 const paint = new CanvasKit.Paint();
1133 paint.setColor(CanvasKit.Color(139, 228, 135, 0.95)); // greenish
1134 paint.setStyle(CanvasKit.PaintStyle.Fill);
1135 paint.setAntiAlias(true);
1137 const subCanvas = subSurface.getCanvas();
1138 subCanvas.clear(CanvasKit.BLACK);
1139 subCanvas.drawRect(CanvasKit.LTRBRect(5, 15, 45, 40), paint);
1141 paint.setColor(CanvasKit.Color(214, 93, 244)); // purplish
1142 for (let i = 0; i < 10; i++) {
1143 const x = Math.random() * 50;
1144 const y = Math.random() * 50;
1146 subCanvas.drawOval(CanvasKit.XYWHRect(x, y, 6, 6), paint);
1149 // Snap it off as an Image - this image will be in the form the
1150 // parent surface prefers (e.g. Texture for GPU / Raster for CPU).
1151 const img = subSurface.makeImageSnapshot();
1153 // clean up the temporary surface (which also cleans up subCanvas)
1154 subSurface.delete();
1157 // Make it repeat a bunch with a shader
1158 const pattern = img.makeShaderCubic(CanvasKit.TileMode.Repeat, CanvasKit.TileMode.Mirror,
1160 const patternPaint = new CanvasKit.Paint();
1161 patternPaint.setShader(pattern);
1165 function drawFrame(canvas) {
1167 canvas.clear(CanvasKit.WHITE);
1169 canvas.drawOval(CanvasKit.LTRBRect(i % 60, i % 60, 300 - (i% 60), 300 - (i % 60)), patternPaint);
1170 surface.requestAnimationFrame(drawFrame);
1172 surface.requestAnimationFrame(drawFrame);
1175 function AtlasAPI1(CanvasKit, imgData) {
1176 if (!CanvasKit || !imgData) {
1179 const surface = CanvasKit.MakeCanvasSurface('atlas');
1181 console.error('Could not make surface');
1184 const img = CanvasKit.MakeImageFromEncoded(imgData);
1186 const paint = new CanvasKit.Paint();
1187 paint.setColor(CanvasKit.Color(0, 0, 0, 0.8));
1189 // Allocate space for 2 rectangles.
1190 const srcs = CanvasKit.Malloc(Float32Array, 8);
1191 srcs.toTypedArray().set([
1192 0, 0, 250, 250, // LTRB
1196 // Allocate space for 2 RSXForms
1197 const dsts = CanvasKit.Malloc(Float32Array, 8);
1198 dsts.toTypedArray().set([
1199 .5, 0, 0, 0, // scos, ssin, tx, ty
1203 // Allocate space for 4 colors.
1204 const colors = new CanvasKit.Malloc(Uint32Array, 2);
1205 colors.toTypedArray().set([
1206 CanvasKit.ColorAsInt( 85, 170, 10, 128), // light green
1207 CanvasKit.ColorAsInt( 51, 51, 191, 128), // light blue
1212 function drawFrame(canvas) {
1213 canvas.clear(CanvasKit.WHITE);
1215 let scale = 0.5 + Math.sin(i/40)/4;
1217 // update the coordinates of existing sprites - note that this
1218 // does not require a full re-copy of the full array; they are
1219 // updated in-place.
1220 dsts.toTypedArray().set([0.5, 0, (2*i)%200, (5*Math.round(i/200)) % 200], 0);
1221 dsts.toTypedArray().set([scale*Math.sin(i/20), scale*Math.cos(i/20), 200, 100], 4);
1223 canvas.drawAtlas(img, srcs, dsts, paint, CanvasKit.BlendMode.Plus, colors,
1224 {filter: CanvasKit.FilterMode.Nearest});
1225 surface.requestAnimationFrame(drawFrame);
1227 surface.requestAnimationFrame(drawFrame);
1231 async function DecodeAPI(CanvasKit, imgData) {
1232 if (!CanvasKit || !imgData) {
1235 const surface = CanvasKit.MakeCanvasSurface('decode');
1237 console.error('Could not make surface');
1240 const blob = new Blob([ imgData ]);
1241 // ImageBitmap is not supported in Safari
1242 const imageBitmap = await createImageBitmap(blob);
1243 const img = await CanvasKit.MakeImageFromCanvasImageSource(imageBitmap);
1245 surface.drawOnce((canvas) => {
1246 canvas.drawImage(img, 0, 0, null);