美文网首页
js canvas操作图片

js canvas操作图片

作者: 7f6a30f82e2a | 来源:发表于2021-09-30 14:26 被阅读0次

// canvas工具类

// 画图片 参数:图片地址 x坐标 y坐标 宽 高 回调函数

  // 回调函数:如果传了回调函数会返回图片的dom 第二个参数为next-不执行不会往下走

  // next可穿绘制图片的宽度和高度-不传为默认传的宽度和高度,都不传为默认的图片宽高

可以制作类似微信为头像添加党徽的功能,案例在下方

<!DOCTYPE html>

<html>

<head>

  <title></title>

</head>

<body>

<input id="inputFile" accept="image/*" type="file" />

<input id="inputFile2" accept="image/*" type="file" />

<div>

  <canvas id="mycanvas"></canvas>

</div>

<script src="./utilityClass.js"></script>

<script type="text/javascript">

const color = new ColorProcessing('#46a6ff')

console.log(color)

const canvasHandler = new CanvasHandler('#mycanvas')

const fileHandler = new FileHandler()

inputFile.addEventListener('change', async function () {

  const imgArr = await fileHandler.fileToBase64(this.files)

  canvasHandler.drawImage(imgArr[0], 0, 0, 0, 0, (info, next) => {

    let width = info.width

    let height = info.height / (width / 200)

    canvasHandler.canvas.width = 200

    canvasHandler.canvas.height = height

    next(200, height)

  })

})

inputFile2.addEventListener('change',async  function () {

  const imgArr = await fileHandler.fileToBase64(this.files)

  canvasHandler.drawImage(imgArr[0], 0, 0, 0, 0, (info, next) => {

    let height = info.height

    let width = info.width / (height / mycanvas.height)

    next(width, canvasHandler.canvas.height)

  })

})

</script>

</body>

</html>

相关文章

网友评论

      本文标题:js canvas操作图片

      本文链接:https://www.haomeiwen.com/subject/pcsenltx.html