How can I include a SVG image in a HTML5 canvas element, so that it also adjusts the size of the SVG when you change the size of the browser window?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
-
Why does the box-shadow property not apply to a
This requires quite some boilerplate code to demonstrate so I'm just gonna point you in the right direction.
Say you have access to a canvas object/element named
myCanvas
. When you executevar ctx = myCanvas.getContext('2d')
thenctx
is an instance ofCanvasRenderingContext2D
. Now when you draw your imageimg
you executectx.drawImage(img, dx, dy, sw, sh)
where:dx
anddy
is the offset from the top-left corner.sw
andsh
is the absoulte size of the image.So, you adjust the image size with
sw
andsh
. You want them to depend on the canvas size which you can access withmyCanvas.height
andmyCanvas.width
.You want the canvas width/height to depend on window size. Window size is accessed with
window.innerWidth
andwindow.innerHeight
. When you resize you can listen to this event like this:window.addEventListener('resize', function (evt) { /* handle resize here */ });
. Example: