简体   繁体   English

如何在html js中制作可滚动的画布图像?

[英]How to make scrollable canvas image in html js ?

I'm trying to make a web page with canvas image. 我正在尝试使用画布图像制作网页。 Canvas image should be scrollable (x, y). 画布图像应该是可滚动的(x,y)。 But my code does vertical scroll only not the horizontal. 但是我的代码只垂直滚动而不是水平滚动。

html html

<nav>
<main>
  <canvas id="floornet"></canvas>
</main>

js 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) 
  }
}

As specified here , you can achieve that as follows: 作为指定在这里 ,你可以做到这一点,如下所示:

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM