admin管理员组文章数量:1414935
i´m working on a WebApp where Users can draw on a Canvas with their Mobile Phones and Tablets. My Problem is that if i specify the size of the canvas as percentage value in CSS, the TouchDraw-Script () is not working correctly anymore. Which is sad, because otherwise it´s perfect.
So i figured that i specify width and height of the canvas directly in the Script like this:
sigCanvas.setAttribute('width', '320');
sigCanvas.setAttribute('height', '480');
Of course i have to deal with different Screen Resolutions, so a Percentage Value would be best. Is there a way to use percentage value in my code? Cause this
sigCanvas.setAttribute('width', '90%');
is not working. Thanks in advance!
i´m working on a WebApp where Users can draw on a Canvas with their Mobile Phones and Tablets. My Problem is that if i specify the size of the canvas as percentage value in CSS, the TouchDraw-Script (http://www.codeproject./Articles/355230/HTML-5-Canvas-A-Simple-Paint-Program-Touch-and-Mou) is not working correctly anymore. Which is sad, because otherwise it´s perfect.
So i figured that i specify width and height of the canvas directly in the Script like this:
sigCanvas.setAttribute('width', '320');
sigCanvas.setAttribute('height', '480');
Of course i have to deal with different Screen Resolutions, so a Percentage Value would be best. Is there a way to use percentage value in my code? Cause this
sigCanvas.setAttribute('width', '90%');
is not working. Thanks in advance!
Share Improve this question asked Mar 5, 2013 at 9:52 user2135133user2135133 231 silver badge3 bronze badges1 Answer
Reset to default 3Ok, so you can do what you want by attaching to the window resize event like so:
$(window).resize(callbackFunc);
Here are the changes that I made to get it working.
See on jsFiddle
$(document).ready(function () {
initialize();
updateSize();
});
$(window).resize(updateSize);
function updateSize() {
var sigCanvas = document.getElementById("canvasSignature");
var xOffset = 100;
var yOffset = 100; // header height
sigCanvas.setAttribute('width', window.innerWidth - xOffset);
sigCanvas.setAttribute('height', window.innerHeight - yOffset);
}
The xOffset
and yOffset
can be customised to your needs, we need to subtract from the width and height of the window otherwise the canvas will go off the page. Also make sure you run it when the page loads so the size is adjusted to the correct size then.
本文标签: javascriptsetAttribute width for canvas as percentage valueStack Overflow
版权声明:本文标题:javascript - setAttribute width for canvas as percentage value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745156608a2645211.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论