OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Sizing SVG image when drawn to canvas</title> |
| 3 <script> |
| 4 function createCanvasWithImage(imgSrc, drawFunc) |
| 5 { |
| 6 var canvas = document.createElement('canvas'); |
| 7 canvas.width = 100; |
| 8 canvas.height = 100; |
| 9 var img = document.createElement('img'); |
| 10 img.src = imgSrc; |
| 11 img.onload = function() { |
| 12 drawFunc(canvas.getContext('2d'), img); |
| 13 document.documentElement.removeChild(img); |
| 14 } |
| 15 document.documentElement.appendChild(img); |
| 16 document.documentElement.appendChild(canvas); |
| 17 } |
| 18 |
| 19 createCanvasWithImage( |
| 20 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10
10"><circle cx="5" cy="5" r="5" fill="blue"/></svg>', |
| 21 function(ctx, img) { |
| 22 ctx.drawImage(img, 0, 0); |
| 23 }); |
| 24 |
| 25 createCanvasWithImage( |
| 26 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20
10"><circle cx="10" cy="5" r="5" fill="blue"/></svg>', |
| 27 function(ctx, img) { |
| 28 ctx.drawImage(img, 0, 0); |
| 29 }); |
| 30 |
| 31 createCanvasWithImage( |
| 32 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10
20"><circle cx="5" cy="10" r="5" fill="blue"/></svg>', |
| 33 function(ctx, img) { |
| 34 ctx.drawImage(img, 0, 0); |
| 35 }); |
| 36 |
| 37 createCanvasWithImage( |
| 38 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20
10"><circle cx="10" cy="5" r="5" fill="blue"/></svg>', |
| 39 function(ctx, img) { |
| 40 ctx.drawImage(img, 0, 0, 100, 100); |
| 41 }); |
| 42 |
| 43 createCanvasWithImage( |
| 44 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10
20"><circle cx="5" cy="10" r="5" fill="blue"/></svg>', |
| 45 function(ctx, img) { |
| 46 ctx.drawImage(img, 0, 0, 100, 100); |
| 47 }); |
| 48 |
| 49 createCanvasWithImage( |
| 50 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="50" viewB
ox="0 0 10 20"><circle cx="5" cy="10" r="5" fill="blue"/></svg>', |
| 51 function(ctx, img) { |
| 52 ctx.drawImage(img, 0, 0); |
| 53 }); |
| 54 |
| 55 createCanvasWithImage( |
| 56 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" height="50" view
Box="0 0 20 10"><circle cx="10" cy="5" r="5" fill="blue"/></svg>', |
| 57 function(ctx, img) { |
| 58 ctx.drawImage(img, 0, 0); |
| 59 }); |
| 60 |
| 61 createCanvasWithImage( |
| 62 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" heig
ht="50"><circle cx="50" cy="25" r="25" fill="blue"/></svg>', |
| 63 function(ctx, img) { |
| 64 ctx.drawImage(img, 0, 0); |
| 65 }); |
| 66 </script> |
OLD | NEW |