I am currently working on a project involving the canvas element and have come across a problem when I resize the canvas. I'm trying to draw an image at the center of the canvas by dividing the width and height by 2, but whenever I resize the canvas, it only changes the HTML, so when I go to redraw the image, it still draws at half the width and height of the old canvas. I'm not sure if this is a DOM issue or what, but would appreciate any help. I have tested in Safari, FF, and Chrome
Edit: Code
<body>
<div id="holder">
<div id="dropdown">
<div id="ddcontents" style="display: none;">
<button onclick="fade()">Options</button>
<button onclick="getSize()">Canvas Size</button>
<button onclick="redraw()" disabled="true">Redraw</button>
</div>
</div>
<canvas id="c" height="480" width="640">Your browser does not support HTML5.</canvas>
<div id="options" style="display: none">
<div id="optcontent" style="display: none">
<center><h1>Options</h1></center>
<div id="sound">
<div>Volume:
<form oninput="volumenumber.value = parseInt(volume.value)">
<input type="range" name="volume" min="0" max="100" value="80" disabled="true">
<input type="hidden" name="hello" value="%">
<output name="volumenumber" for="volume">80</output>
</form>
Resolution:
<select id="resolution">
<option value="480p">480x640</option>
<option value="720p">720x1280</option>
</select>
</div>
</div>
<div id="closeoptions">
<button onclick="apply()">Apply</button>
</div>
</div>
</div>
</div>
<script src="options.js"></script>
</body>
</html>
And for the JS:
var c = document.getElementById('c');
var ctx=c.getContext("2d");
var img=new Image();
var w = this.c.clientWidth; // stores the width for later use
var h = this.c.clientHeight;
this.c.width = w;
this.c.height = h;
var cwidth = c.width;
var cheight = c.height;
img.onload = function(){
ctx.drawImage(img,w/2 - 14,h/2 - 19);
};
img.src="image_preview.png";
function apply()
{
var reso = document.getElementById("resolution");
var resol = reso.options[reso.selectedIndex].value;
//alert(resol)
if (resol == "720p")
{
//alert("You changed to 720p");
h = 720;
w = 1280;
cheight = 720;
cwidth = 1280;
}
else if (resol == "480p")
{
//alert("You changed to 480p");
h = 480;
w = 640;
cheight = 480;
cheight = 640;
}
getSize();
redraw();
}
function redraw()
{
ctx.drawImage(img,w/2 - 14,h/2 - 19);
img.src="image_preview.png";
}
function getSize()
{
alert(h + "x" + w);
}
When using a canvas you have to ensure its internal structure has the same size of the rendering html zone.
You can do that :
this.canvas.width = this.canvas.clientWidth;
this.canvas.height = this.canvas.clientHeight;
Usually I also store those dimensions for my rendering computations :
var w = this.canvas.clientWidth; // stores the width for later use
var h = this.canvas.clientHeight;
this.canvas.width = w;
this.canvas.height = h;
EDIT : to change the element size, do this kind of thing before the precedent code :
$('#mycanvas').width(newWidthInPx);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.