【发布时间】:2014-11-04 15:19:27
【问题描述】:
我有一个可以在上面绘制图像的画布。 如果我通过 css in % 设置画布大小,那么图像会变得非常模糊。 如果我将其设置为像素,则图像绘制得很好。
#myCanvas{
border:1px dotted black;
width:100%;
}
img{
width:100%;
height:150px;
}
<canvas id="myCanvas">Your Phone is Not Compatible With the APP.</canvas>
<p></p>
<img src="http://www.artlimited.net/user/0/0/1/4/9/7/9/artlimited_img175960.jpg">
$("document").ready(function(){
function drawcanvas(){
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.src ="http://www.artlimited.net/user/0/0/1/4/9/7/9/artlimited_img175960.jpg";
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0,context.canvas.width,context.canvas.height);
document.querySelector("p").innerHTML="canvas height="+context.canvas.height+"canvas width="+context.canvas.height;
};
}
drawcanvas();
});
【问题讨论】:
-
我需要提高图像的分辨率,因为你看到它非常糟糕
-
使用js设置画布的宽高。 canvas 元素在绘制时不会考虑 % 值。width 属性默认为 300,height 属性默认为 150..
-
PS:在
onload函数后面设置src,也可以用canvas.width代替context.canvas.width。
标签: javascript html canvas