javascript - 如何在 html js 中制作可滚动的画布图像?
问题描述
我正在尝试使用画布图像制作网页。画布图像应该是可滚动的(x,y)。但是我的代码只做垂直滚动而不是水平滚动。
html
<nav>
<main>
<canvas id="floornet"></canvas>
</main>
js
window.onload = function() {
// Get the image
const image = new Image()
image.src = 'https://upload.wikimedia.org/wikipedia/commons/3/36/Hopetoun_falls.jpg'
// Wait Till the Image is loaded
image.onload = function() {
drawCanvas(image)
}
function drawCanvas(image) {
// Create canvas context
const canvas = document.getElementById('floornet')
const context = canvas.getContext("2d")
canvas.width = image.height
canvas.height = image.height
// Draw image to the canvas
context.drawImage(image, 0, 0)
}
}
解决方案
如指定here,您可以按如下方式实现:
window.onload = function() {
// Get the image
const image = new Image()
image.src = 'https://upload.wikimedia.org/wikipedia/commons/3/36/Hopetoun_falls.jpg'
// Wait Till the Image is loaded
image.onload = function() {
drawCanvas(image)
}
function drawCanvas(image) {
// Create canvas context
const canvas = document.getElementById('floornet')
const context = canvas.getContext("2d")
canvas.width = image.height
canvas.height = image.height
// Draw image to the canvas
context.drawImage(image, 0, 0)
}
}
<main>
<div style="max-height: 256px;max-width:256px;overflow: scroll;">
<canvas width="512px" height="512px" id="floornet"></canvas>
</div>
</main>
推荐阅读
- android - React Native ActivityIndicator 不显示
- bash - 在 cygwin 终端中将文件的内容直接放入剪贴板
- physics - 模拟一系列具有各种质量和耦合函数的旋转顶点
- c# - 有没有办法在 lambda 表达式的 where 条件中使用 forEach?
- c++ - C++ STD::List:遍历列表时遇到问题
- rust - 如何为外国类型生成我的特征的实现?
- python-3.x - X 平均标准偏差的值需要找到曲线下的概率或面积
- swift - SwiftUI:如何使用语义放置呈现多个 ToolbarItem?
- c# - 调整大小控制闪烁
- python - groupby 和 between 的复杂功能?Python