缩放开始
<html>
<head> </head>
<body>
<canvas
id="canvas"
style="display:block;margin:0 auto;border:1px solid #ccc"
>您的浏览器不支持canvas</canvas
>
<input
type="range"
id="scale-range"
min="0.5"
max="3.0"
step="0.01"
value="1.0"
style="display:block;margin:20px auto;width:800px;"
/>
</body>
</html>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas')
canvas.width = 800
canvas.height = 800
var context = canvas.getContext('2d')
var slider = document.getElementById('scale-range')
var scale = slider.value
var img = new Image()
img.src = 'xxx.jpg'
img.onload = function () {
drawImageByScale(scale)
slider.onmousemove = function () {
scale = slider.value
drawImageByScale(scale)
}
}
}
function drawImageByScale(scale) {
var imageWidth = 1152 * scale
var imageHeight = 768 * scale
var dx = canvas.width / 2 - imageWidth / 2
var dy = canvas.height / 2 - imageHeight / 2
context.clearRect(0, 0, canvas.width, canvas.height)
context.drawImage(img, dx, dy, imageWidth, imageHeight)
}
</script>