如何将百分比宽度放入 html 画布(无 css) [英] How put percentage width into html canvas (no css)

查看:15
本文介绍了如何将百分比宽度放入 html 画布(无 css)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,并在它上面覆盖了一个画布,这样我就可以在不修改图像本身的情况下在"图像上绘制.

<img id="image" src=""/><!-- 必须在 html 中指定画布大小 --><canvas id="sketchpad" width="70%" height="60%">抱歉,您的浏览器不受支持.</canvas>

我可以在上面的画布行中以像素为单位指定宽度和高度,效果很好,但是我需要根据屏幕大小动态调整大小(必须在小型智能手机和平板电脑上工作).当我尝试按如上所示输入百分比时,它会将其解释为像素.因此,画布将是 70 像素宽和 60 像素高.

出于某种原因,我无法在 CSS 中调整画布的大小,所以看起来我必须在 html 中进行调整.澄清一下,当我尝试在 CSS 中指定画布尺寸时,它们实际上并没有改变画布的大小.图像似乎以某种方式干扰了.

任何帮助将不胜感激.

更新:如果我这样做 </canvas> 然后默认为 150 像素的高度和 300 像素的宽度(无论设备如何),然后 拉伸 画布以适合 div.我在 css 中将 div 设置为 60% 的宽度和 60% 的高度,因此画布会拉伸以填充它.我通过记录 canvas.width 与 canvas.style.width 来确认这一点——canvas.width 为 300px,canvas.sytle.width 为60%"(来自父 div).这会导致一些非常奇怪的像素化和绘图效果......

解决方案

您需要以像素为单位设置画布的大小,否则不仅会降低其内容的质量,而且如果您想在其上绘制鼠标-坐标也将关闭.如果您打算以交互方式使用它,请不要使用 CSS 来缩放画布.

我建议您调整您的图像,然后将画布调整为任何图像大小.您可以通过使用 getComputedStyle(element) 来完成此操作,它以像素为单位为您提供元素设置的任何大小.

在图像的 onload 之后(因为您需要确保图像实际加载以获取其大小)或稍后如果您在旅途中更改图像 (CSS) 大小,您可以执行以下操作:

////获取图像的计算样式var img = document.getElementById('myImageId');var cs = getComputedStyle(img);///这些将返回以 *pixel* 为单位的尺寸,不管是什么///您最初为图像指定:var width = parseInt(cs.getPropertyValue('width'), 10);var height = parseInt(cs.getPropertyValue('height'), 10);///现在将其用作画布元素的宽度和高度:var canvas = document.getElementById('myCanvasId');canvas.width = 宽度;canvas.height = 高度;

现在画布将适合图像,但画布像素比为 1:1,可帮助您避免在画布上绘图时出现问题.否则,您还需要转换/缩放鼠标/触摸坐标.

I have an image and have overlaid a canvas on top of it so that I can draw 'on' the image without modifying the image itself.

<div class="needPic" id="container">
    <img id="image" src=""/>
    <!-- Must specify canvas size in html -->
    <canvas id="sketchpad" width="70%" height="60%">Sorry, your browser is not supported.</canvas>
</div>

I can specify the width and height in pixels in the above canvas line and it works great, however I need this to dynamically sized based on screen size (has to work on small smartphones as well as tablets). When I try to put the percentage in as shown above, it interprets it as pixels. Thus, the canvas will be 70px wide and 60 px tall.

For some reason I can't size the canvas in the CSS so looks like I have to do it in the html. To clarify, when I try to specify the canvas dimensions in the CSS they don't actually change the size of the canvas. It seems as if the image is interfering in some way.

Any help would be appreciated.

Update: If I do <canvas id="sketchpad" style="width:70%;height:60%;"></canvas> then it defaults to a height of 150px and width of 300px (regardless of device) and then stretches the canvas to fit the div. I set the div to be 60% width and 60% height in the css, thus the canvas stretches to fill that. I confirmed this by logging the canvas.width vs canvas.style.width -- canvas.width was 300px and canvas.sytle.width was '60%' (from the parent div). This causes some really strange pixelation and drawing effects...

解决方案

You need to set the size of the canvas in pixels or you will not only reduce the quality of its content but if you want to draw on it the mouse-coordinates will be off as well. Don't use CSS to scale canvas if you're gonna use it interactively.

I would recommend you adjust your image then adopt the canvas to whatever size the image is. You can do this by using getComputedStyle(element) which gives you whatever size the element is set to in pixels.

After image's onload (as you need to be sure the image is actually loaded to get its size) or later if you change the image (CSS) size on the go, you can do this:

/// get computed style for image
var img = document.getElementById('myImageId');
var cs = getComputedStyle(img);

/// these will return dimensions in *pixel* regardless of what
/// you originally specified for image:
var width = parseInt(cs.getPropertyValue('width'), 10);
var height = parseInt(cs.getPropertyValue('height'), 10);

/// now use this as width and height for your canvas element:
var canvas = document.getElementById('myCanvasId');

canvas.width = width;
canvas.height = height;

Now the canvas will fit image but with canvas pixel ratio 1:1 to help you avoid problems when you're gonna draw on the canvas. Otherwise you will need to convert/scale the mouse / touch coordinates as well.

这篇关于如何将百分比宽度放入 html 画布(无 css)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆