我正在尝试将图像加载到画布中,我的目标是提供两个功能。如果画布内的图像在任何维度上都更大。我希望能够
1)要么调整它
2) 或默认有滚动条。
所以我在我的画布和我提到的css中添加了一个div overflow:auto
。但我无法滚动整个图像。我正在粘贴我的整个 html 文件以供您试用。请告诉我如何使这项工作。
<head>
<title>resize and scroll</title>
<meta charset="utf-8">
</head>
<body>
<div id="scroll">
<canvas id="canvas" width="800" height="400"></canvas>
</div>
<input type="button" value=" Resize " onclick="resize()">
<input type="button" value="original" onclick="window.onload()">
<style>
#canvas {
border: 1px solid black;
}
#scroll {
width:800;
height:400;
overflow:auto;
border: solid 1px white;
background-color:white;
}
</style>
<script>
window.onload = function() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
lenna = new Image();
lenna.src = 'http://lunar.thegamez.net/greenenergyimage/biomass-energy-resources/biomass-resources-map-agricultural-residues-resourcesjpg-1200x1000.jpg';
lenna.onload=function() {
context.drawImage(lenna,0,0,lenna.width,lenna.height);
}
}
resize = function() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.drawImage(lenna,0,0,canvas.width,canvas.height);
}
</script>
</body>
</html>