成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

利用html2canvas實(shí)現(xiàn)移動(dòng)端上傳圖片生成海報(bào)

Cruise_Chan / 2445人閱讀

摘要:所以需要在這里做一個(gè)判斷。使用的內(nèi)聯(lián)樣式時(shí)遇上的寫(xiě)法問(wèn)題直接使用手機(jī)拍照得到的圖片方向有問(wèn)題

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

主要功能

上傳圖片

對(duì)圖片進(jìn)行操作:移動(dòng)、放大、縮小

合成海報(bào)

具體功能: 上傳圖片

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() { // 獲取圖像的數(shù)據(jù)
                Exif.getAllTags(this); // 獲取圖像的全部數(shù)據(jù),值以對(duì)象的方式返回
                orientation = Exif.getTag(this, "Orientation"); // 獲取圖像的拍攝方向
                var rotateCanvas = document.createElement("canvas"),
                rotateCtx = rotateCanvas.getContext("2d");
                 // 針對(duì)圖像方向進(jìn)行處理
                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 : // 順時(shí)針 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);
                });
            })
        }) 
    })
}
移動(dòng)圖片

對(duì)圖片和相框綁定@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("請(qǐng)上傳圖片")
    }
}
遇到的問(wèn)題

1) 在瀏覽器上阻止縮放的問(wèn)題

在tounchmove時(shí)使用event.preventDefault()

2) 合成圖片的清晰度

在html2canvas寫(xiě)參數(shù)時(shí),scale = window.devicePixelRatio

3) 對(duì)圖片進(jìn)行縮放時(shí),對(duì)距離的判斷

這里需要區(qū)分兩種情況:
- 兩指縮放
- 一指移動(dòng)  
在兩指縮放后會(huì)有一個(gè)手放手另一個(gè)手繼續(xù)操作的情況。這樣在touchend時(shí),e.touches還存在一個(gè)元素。所以需要在這里做一個(gè)判斷。

4) 使用vue的:style="內(nèi)聯(lián)樣式"時(shí)遇上的寫(xiě)法問(wèn)題

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

5)直接使用手機(jī)拍照得到的圖片方向有問(wèn)題

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/109610.html

相關(guān)文章

  • 基于canvas生成圖片

    摘要:本文將介紹如何生成一張海報(bào)圖片,以及可能會(huì)碰到的問(wèn)題和解決方案。轉(zhuǎn)圖片目前移動(dòng)端瀏覽器對(duì)于的支持非常好,而可以通過(guò)來(lái)轉(zhuǎn)換成圖片。 隨著APP的獲客成本越來(lái)越高,很多產(chǎn)品開(kāi)始從wap頁(yè)引流,而最常見(jiàn)的方式便是分享,尤其是在微信中。因此誕生了一些新玩法,比如生成一張海報(bào)圖片,用戶可以保存或分享到其他平臺(tái)。 本文將介紹如何生成一張海報(bào)圖片,以及可能會(huì)碰到的問(wèn)題和解決方案。 canvas轉(zhuǎn)圖片...

    calx 評(píng)論0 收藏0
  • Canvas繪圖在微信小程序中的應(yīng)用:生成個(gè)性化海報(bào)

    摘要:解析進(jìn)到首頁(yè)其實(shí)關(guān)鍵字在本地就隨機(jī)取完了,在首頁(yè)中的方法中就通過(guò)緩存了要畫(huà)的元素,比如關(guān)鍵字這里是圖片關(guān)鍵字解析語(yǔ)也是圖片畢竟微信小程序的不支持字體等等。 一、Canvas應(yīng)用的背景(個(gè)人理解)及基礎(chǔ)語(yǔ)法 背景 從2012年開(kāi)始,微信那個(gè)時(shí)候用戶的積累的量已經(jīng)非常大了,推出公眾號(hào),當(dāng)然大屏智能手機(jī)在那個(gè)時(shí)候也流行,傳統(tǒng)的大眾媒體逐步消亡,像微信公眾號(hào)這樣的新媒體盛行。企業(yè)的廣告投入開(kāi)始...

    vpants 評(píng)論0 收藏0
  • 用小程序·云開(kāi)發(fā)打造功能全面的博客小程序丨實(shí)戰(zhàn)

    摘要:用小程序云開(kāi)發(fā)將博客小程序常用功能一網(wǎng)打盡本文介紹博客小程序的詳情頁(yè)的功能按鈕如何實(shí)現(xiàn),具體包括評(píng)論點(diǎn)贊收藏和海報(bào)功能,這里記錄下整個(gè)實(shí)現(xiàn)過(guò)程和實(shí)際編碼中的一些坑??紤]到小程序本身的大小限制,使用的方式是最佳的。 用小程序·云開(kāi)發(fā)將博客小程序常用功能一網(wǎng)打盡 本文介紹mini博客小程序的詳情頁(yè)的功能按鈕如何實(shí)現(xiàn),具體包括評(píng)論、點(diǎn)贊、收藏和海報(bào)功能,這里記錄下整個(gè)實(shí)現(xiàn)過(guò)程和實(shí)際編碼中的一...

    cc17 評(píng)論0 收藏0
  • 用小程序·云開(kāi)發(fā)打造功能全面的博客小程序丨實(shí)戰(zhàn)

    摘要:用小程序云開(kāi)發(fā)將博客小程序常用功能一網(wǎng)打盡本文介紹博客小程序的詳情頁(yè)的功能按鈕如何實(shí)現(xiàn),具體包括評(píng)論點(diǎn)贊收藏和海報(bào)功能,這里記錄下整個(gè)實(shí)現(xiàn)過(guò)程和實(shí)際編碼中的一些坑??紤]到小程序本身的大小限制,使用的方式是最佳的。 用小程序·云開(kāi)發(fā)將博客小程序常用功能一網(wǎng)打盡 本文介紹mini博客小程序的詳情頁(yè)的功能按鈕如何實(shí)現(xiàn),具體包括評(píng)論、點(diǎn)贊、收藏和海報(bào)功能,這里記錄下整個(gè)實(shí)現(xiàn)過(guò)程和實(shí)際編碼中的一...

    flybywind 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<