html2canvas的使用以及Cordova微信分享图片

1.截屏插件html2canvas

npm install html2canvas --savehtml

使用

import html2canvas from 'html2canvas'
html2canvas(el,options).then(canvas=>{})npm

实例

           html2canvas(document.getElementById('box'),{
                backgroundColor:null,
                useCORS:true
           }).then((canvas)=>{
                this.dataURL = canvas.toDataURL("image/jpeg",1)
                console.log(this.dataURL)
           })

截取出某个div内部的样式(box),将他们绘画成为canvascanvas

常见问题:

1.图片不清晰:跨域

须要将背景图片换为img图片微信

2.请求的数据图片加载不上this

须要设置跨域spa

useCORS:true插件

3.文字不清晰:code

查看百度便可htm

2.cordova微信分享图片

            html2canvas(document.getElementById('box'),{
                backgroundColor:null,
                useCORS:true
            }).then((canvas)=>{
                this.dataURL = canvas.toDataURL("image/jpeg",1)
                console.log(this.dataURL)
                Wechat.share({
                    message:{
                        title:'text',
                        description:'this is description',
                        thumb: "",//分享时候能够看到的图
                        media:{
                            type:Wechat.Type.IMAGE,
                            image:this.dataURL,
                        }
                    },
                    scene:Wechat.Scene.SESSION//分享到好友
                      scene:Wechat.Scene.TIMELINE//分享到朋友圈
                },()=>{
                    alert('success')
                },()=>{
                    alert('error')
                })
            })
相关文章
相关标签/搜索