上面加载了两张本地图片,两张异地图片,写了一些文字;在windows谷歌浏览器跑是好的,是吧,图片画出来,感觉无压力;用安卓也是好的,很开心;可是到IOS手机上,我去,怎么图片显示不出来啊,然后
try catch 错误,没啥有用的信息;
<script>
export default {
data () { // 参数
const u = navigator.userAgent // ios终端
const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
return { // 返回参数
localUrl: isIOS ? location.href.split('#')[0] : location.href, // 当前路径
canvas: Object // canvas对象
}
},
mounted () {
this.initCanvas()
},
methods: {
/**
* 隐藏海报
*/
hidePoster () {
this.$emit('hide')
},
/**
* 加载图片
* @param {Object} img 图片地址
* @return {Promise} img dom
*/
loadImage (img) {
return new Promise((resolve, reject) => {
// image dom 对象
const $image = document.createElement('img')
if (img.isCross_domain) {
console.log(img.url)
$image.setAttribute('crossOrigin', 'Anonymous')
}
$image.onload = () => {
resolve($image)
}
$image.src = img.url
$image.onerror = reject
})
},
/**
* init初始化canvas函数
*/
async initCanvas () {
// 获取vue实例
var vm = this
vm.$indicator.open({
text: '加载中...',
spinnerType: 'fading-circle'
})
this.canvas = this.$refs.canvas.getContext('2d')
this.canvas.height = 400
this.canvas.width = 300
this.canvas.fillStyle = '#ffffff'
this.canvas.fillRect(0, 0, 588, 1044)
// image urls
const imgArr = [
{
url: require('../assets/poster-banner.png'),
isCross_domain: false
},
{
url: require('../assets/shadow.png'),
isCross_domain: false
},
{
url: 'https://s3-011-shinho-syj-uat-bjs.s3.cn-north-1.amazonaws.com.cn/mall/2019_06/border04.png',
isCross_domain: true
},
{
url: 'https://s3-011-shinho-syj-uat-bjs.s3.cn-north-1.amazonaws.com.cn/mall/2019_06/132.jpg',
isCross_domain: true
}
]
// image doms
await Promise.all(imgArr.map(img => this.loadImage(img))).then((imgs) => {
console.log('done')
this.canvas.drawImage(imgs[0], 0, 0, 588, 216 * 2)
this.canvas.drawImage(imgs[1], 97 * 2, 166 * 2, 100 * 2, 100 * 2)
this.canvas.save()
this.canvas.beginPath()
this.canvas.arc(147 * 2, 214 * 2, 34 * 2, 0, 2 * Math.PI, false)
this.canvas.clip()
this.canvas.drawImage(imgs[2], 113 * 2, 180 * 2, 68 * 2, 68 * 2)
this.canvas.restore()
this.canvas.drawImage(imgs[3], 189 * 2, 409 * 2, 88 * 2, 88 * 2)
// 绘制文字
this.drawText('我就是个我就账号账号', 147 * 2, 278 * 2, 290 * 2, '#333333', '32px PingFangSC-Regular ')
this.drawText('荣誉称号是我', 147 * 2, 300 * 2, 290 * 2, '#999999', '26px PingFangSC-Regular ')
this.drawText('距离冲榜还差10人', 147 * 2, 340 * 2, 290 * 2, '#FA6F5B', 'bold 36px arial')
this.drawText('快来助我冲榜赢红烧酱油吧', 147 * 2, 370 * 2, 290 * 2, '#FA6F5B', 'bold 36px arial ')
this.drawText('扫描二维码', 180 * 2, 443 * 2, 172 * 2, '#333333', '28px PingFangSC-Regular ', 'right')
this.drawText('直达冲榜活动', 180 * 2, 463 * 2, 172 * 2, '#333333', '28px PingFangSC-Regular ', 'right')
this.drawText('邀请好友跟你一起冲大奖', 180 * 2, 483 * 2, 172 * 2, '#333333', '28px PingFangSC-Regular ', 'right')
this.showPic()
vm.$indicator.close()
})
},
/**
* 绘制文字
* @param {String} title 文字名称
* @param {Number} x x轴坐标
* @param {Number} y y轴坐标
* @param {Number} maxwidth 最大宽度
* @param {String} color 颜色
* @param {String} font 字体样式
* @param {String} textalign 文字排版
*/
drawText (title, x, y, maxwidth, color, font, textalign = 'center') {
this.canvas.font = font
this.canvas.textAlign = textalign
this.canvas.fillStyle = color
this.canvas.fillText(title, x, y, maxwidth)
},
/**
* 显示图片
*/
showPic () {
// 获取canvas对象
let canvas = this.$refs.canvas
try {
// 将canvas对象转化为image/png
var dataUrl = canvas.toDataURL('image/png')
} catch (err) {
console.log(err)
}
// 创建img 元素
var newImg = document.createElement('img')
newImg.src = dataUrl
newImg.style.width = '100%'
newImg.style.height = '100%'
newImg.className = 'img-poster'
newImg.style.borderRadius = '8px'
this.$refs.box.appendChild(newImg)
}
}
}
</script>
里面讲了画布的污染和解决方法,就是设置 crossorigin = “Anonymous”;里面的方法也是先设置crossorigin在图片加载完后设置 src;
如下
到此这篇关于关于canvas.toDataURL 在iOS运行失败的问题解决 的文章就介绍到这了,更多相关canvas.toDataURL在iOS运行失败内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!