资讯专栏INFORMATION COLUMN

利用html2canvas实现移动端上传图片生成海报

Cruise_Chan / 2238人阅读

摘要:所以需要在这里做一个判断。使用的内联样式时遇上的写法问题直接使用手机拍照得到的图片方向有问题

原文链接:链接描述
使用vue+html2canvas+exif-js
github地址
线上demo

主要功能

上传图片

对图片进行操作:移动、放大、缩小

合成海报

具体功能: 上传图片

html:

js

getPhoto () {
    var imageInput = document.querySelector("#image-input")
    var that = this
    imageInput.addEventListener("change", function (e) {
        reads = new FileReader()
        reads.readAsDataURL(this.files[0])
        reads.addEventListener("load", function (e) {
            that.imgUrl = this.result
            that.myImg.position.x = 0
            that.myImg.position.y = 0
            that.myImg.scale = 1
            var orientation
            that.previewImg.addEventListener("load", function () {
                Exif.getData(that.previewImg, function() { // 获取图像的数据
                Exif.getAllTags(this); // 获取图像的全部数据,值以对象的方式返回
                orientation = Exif.getTag(this, "Orientation"); // 获取图像的拍摄方向
                var rotateCanvas = document.createElement("canvas"),
                rotateCtx = rotateCanvas.getContext("2d");
                 // 针对图像方向进行处理
                switch (orientation) {
                    case 1 :
                        rotateCanvas.width = that.previewImg.width;
                        rotateCanvas.height = that.previewImg.height;
                        rotateCtx.drawImage(that.previewImg, 0, 0, that.previewImg.width, that.previewImg.height);
                        break;
                    case 6 : // 顺时针 90 度
                        rotateCanvas.width = that.previewImg.height;
                        rotateCanvas.height = that.previewImg.width;
                        rotateCtx.translate(0, 0);
                        rotateCtx.rotate(90 * Math.PI / 180);
                        rotateCtx.drawImage(that.previewImg, 0, -that.previewImg.height, that.previewImg.width, that.previewImg.height);
                        break;
                    case 8 :
                        rotateCanvas.width = that.previewImg.height;
                        rotateCanvas.height = that.previewImg.width;
                        rotateCtx.translate(0, 0);
                        rotateCtx.rotate(-90 * Math.PI / 180);
                        rotateCtx.drawImage(that.previewImg, -that.previewImg.width, 0, that.previewImg.width, that.previewImg.height);
                        break;
                    case 3 : // 180 度
                        rotateCanvas.width = that.previewImg.width;
                        rotateCanvas.height = that.previewImg.height;
                        rotateCtx.translate(0, 0);
                        rotateCtx.rotate(Math.PI);
                        rotateCtx.drawImage(that.previewImg, -that.previewImg.width, -that.previewImg.height, that.previewImg.width, that.previewImg.height);
                        break;
                    default :
                        rotateCanvas.width = that.previewImg.width;
                        rotateCanvas.height = that.previewImg.height;
                        rotateCtx.drawImage(that.previewImg, 0, 0, that.previewImg.width, that.previewImg.height);
                    }
                    var rotateBase64 = rotateCanvas.toDataURL("image/jpeg", 0.5);
                });
            })
        }) 
    })
}
移动图片

对图片和相框绑定@touchstart @touchmove @touchend

getInitPosition (e) {
    event.preventDefault()
    if (this.imgUrl) {
        var length = e.touches.length
         if (length > 1) {
            let pointOne = e.touches[0]
            let pointTwo = e.touches[1]
            this.initTouchX = pointOne.clientX - pointTwo.clientX
            this.initTouchY = pointOne.clientY - pointTwo.clientY
        } else {
            var touches = e.touches[0]
            this.initTouchX = touches.clientX
            this.initTouchY = touches.clientY
        }   
    }
},
 getMovePosition (e) {
    event.preventDefault()
    if (this.imgUrl) {
        var length = e.touches.length
        if (length > 1) {
            let pointOne = e.touches[0]
            let pointTwo = e.touches[1]
            this.changeTouchX = pointOne.clientX - pointTwo.clientX
            this.changeTouchY = pointOne.clientY - pointTwo.clientY
            var scale = (this.changeTouchX - this.initTouchX) > (this.changeTouchY - this.initTouchY) ? (this.changeTouchX / this.initTouchX) : (this.changeTouchY / this.initTouchY) 
            scale *= this.myImg.lastScale
            this.myImg.scale = scale > 3 ? 3 : scale < 0.5 ? 0.5 : scale
        } else {
            var touches = e.touches[0]
            this.changeTouchX = touches.clientX - this.initTouchX
            this.changeTouchY = touches.clientY - this.initTouchY
            this.myImg.position.x = this.lastTouchX + (this.changeTouchX / this.myImg.scale)
            this.myImg.position.y = this.lastTouchY + (this.changeTouchY / this.myImg.scale)
        }   
    }
},
getLeavePosition (e) {
    this.myImg.lastScale = this.myImg.scale
    if (e.touches.length > 0) {
        var touches = e.touches[0]
        this.initTouchX = touches.clientX
        this.initTouchY = touches.clientY
    }
    this.lastTouchX = this.myImg.position.x
    this.lastTouchY = this.myImg.position.y
},
合成图片
createPhoto () {
    if (this.imgUrl) {
        let photoBox = document.querySelector(".photo-box")
        newImgWidth = photoBox.style.offsetWidth
        let newImgHeight = photoBox.style.offsetHeight
        let scale = window.devicePixelRatio
        let that = this
        html2canvas(photoBox, {
            width: newImgWidth,
            height: newImgHeight,
            scale: scale,
            useCORS: true
        }).then(function (canvas) { 
            var dataUrl = canvas.toDataURL("image/jpg")
            localStorage.imgData = dataUrl
            that.$router.push({
                name: "share",
                params: {
                    storage: "imgData"
                }
            })
        })
    } else {
        alert("请上传图片")
    }
}
遇到的问题

1) 在浏览器上阻止缩放的问题

在tounchmove时使用event.preventDefault()

2) 合成图片的清晰度

在html2canvas写参数时,scale = window.devicePixelRatio

3) 对图片进行缩放时,对距离的判断

这里需要区分两种情况:
- 两指缩放
- 一指移动  
在两指缩放后会有一个手放手另一个手继续操作的情况。这样在touchend时,e.touches还存在一个元素。所以需要在这里做一个判断。

4) 使用vue的:style="内联样式"时遇上的写法问题

:style="{transform:"scale("+ myImg.scale+ ") translate("+myImg.position.x+"px,"+myImg.position.y+"px)"}"

5)直接使用手机拍照得到的图片方向有问题

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/109610.html

相关文章

  • 基于canvas生成图片

    摘要:本文将介绍如何生成一张海报图片,以及可能会碰到的问题和解决方案。转图片目前移动端浏览器对于的支持非常好,而可以通过来转换成图片。 随着APP的获客成本越来越高,很多产品开始从wap页引流,而最常见的方式便是分享,尤其是在微信中。因此诞生了一些新玩法,比如生成一张海报图片,用户可以保存或分享到其他平台。 本文将介绍如何生成一张海报图片,以及可能会碰到的问题和解决方案。 canvas转图片...

    calx 评论0 收藏0
  • Canvas绘图在微信小程序中的应用:生成个性化海报

    摘要:解析进到首页其实关键字在本地就随机取完了,在首页中的方法中就通过缓存了要画的元素,比如关键字这里是图片关键字解析语也是图片毕竟微信小程序的不支持字体等等。 一、Canvas应用的背景(个人理解)及基础语法 背景 从2012年开始,微信那个时候用户的积累的量已经非常大了,推出公众号,当然大屏智能手机在那个时候也流行,传统的大众媒体逐步消亡,像微信公众号这样的新媒体盛行。企业的广告投入开始...

    vpants 评论0 收藏0
  • 用小程序·云开发打造功能全面的博客小程序丨实战

    摘要:用小程序云开发将博客小程序常用功能一网打尽本文介绍博客小程序的详情页的功能按钮如何实现,具体包括评论点赞收藏和海报功能,这里记录下整个实现过程和实际编码中的一些坑。考虑到小程序本身的大小限制,使用的方式是最佳的。 用小程序·云开发将博客小程序常用功能一网打尽 本文介绍mini博客小程序的详情页的功能按钮如何实现,具体包括评论、点赞、收藏和海报功能,这里记录下整个实现过程和实际编码中的一...

    cc17 评论0 收藏0
  • 用小程序·云开发打造功能全面的博客小程序丨实战

    摘要:用小程序云开发将博客小程序常用功能一网打尽本文介绍博客小程序的详情页的功能按钮如何实现,具体包括评论点赞收藏和海报功能,这里记录下整个实现过程和实际编码中的一些坑。考虑到小程序本身的大小限制,使用的方式是最佳的。 用小程序·云开发将博客小程序常用功能一网打尽 本文介绍mini博客小程序的详情页的功能按钮如何实现,具体包括评论、点赞、收藏和海报功能,这里记录下整个实现过程和实际编码中的一...

    flybywind 评论0 收藏0

发表评论

0条评论

Cruise_Chan

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<