【发布时间】:2013-11-22 03:47:25
【问题描述】:
我正在尝试使用以下代码在宽度为 150 像素、高度为 150 像素的画布上绘制图片。但是,图片似乎随着我为画布设置的宽度和高度属性缩放(我将宽度设置为 500 像素,高度设置为 300 像素)。为什么画布会这样工作?如何在画布上以 px 为单位绘制绝对大小的图像?
非常感谢!
<head>
<meta charset="UTF-8">
<title>Base</title>
<script type="text/javascript" src="./js/jquery-1.10.1.js"></script>
<script type="text/javascript">
var img = new Image();
$(window).ready(function() {
img.src = "./Images/img.jpg";;
ctx = document.getElementById("main").getContext("2d");
img.onload = function() {
ctx.drawImage(img, 0, 0, 150, 150);
}
});
</script>
</head>
<body>
<canvas id="main" style="background-color:yellow; width:500px; height:300px; z-index:1; position:absolute;"></canvas>
</body>
【问题讨论】: