Canvas(十一) Canvas绘制图像

绘制图像 drawImage

  • drawImage(image,dx,dy)
window.onload = function () {
  var canvas = document.getElementById('canvas')
  canvas.width = 800
  canvas.height = 800
  var context = canvas.getContext('2d')
  var img = new Image()
  img.src = 'xxx.jpg' //注意路径
  img.onload = function () {
    context.drawImage(img, 0, 0)
  }
}
  • drawImage(image,dx,dy,dw,dh)

这样的缩放会全面铺开

window.onload = function () {
  var canvas = document.getElementById('canvas')
  canvas.width = 800
  canvas.height = 800
  var context = canvas.getContext('2d')
  var img = new Image()
  img.src = 'xxx.jpg' //注意路径
  img.onload = function () {
    context.drawImage(img, 0, 0, canvas.width, canvas.height)
  }
}
  • drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh)

对原图像进行操作,我们希望把原图像 sx,sy,sw,sh 这些坐标渲染到画布 dx,dy,dw,dh 上

window.onload = function () {
  var canvas = document.getElementById('canvas')
  canvas.width = 800
  canvas.height = 800
  var context = canvas.getContext('2d')
  var img = new Image()
  img.src = 'xxx.jpg' //注意路径
  img.onload = function () {
    context.drawImage(img, 600, 200, 400, 400, 200, 200, 400, 400)
  }
}

文章作者: 雾烟云
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 雾烟云 !
  目录