const canvas = document.getElementById('canvasImg')
const context = canvas.getContext('2d')
context.clearRect(0, 0, canvas.width, canvas.height)
let imgSrc = window.URL.createObjectURL(file)// 获取file文件路径
const img = new Image()
img.width = that.graphOptionsWidth
img.height = that.graphOptionsHeight
img.src = imgSrc
img.onload = () => {
context.drawImage(img, 0, 0, that.graphOptionsWidth,
that.graphOptionsHeight)// 这里用image对象,不能用base64路径
function dataURLtoFile (dataURI, type) {
let binary = atob(dataURI.split(',')[1])
let array = []
for (let i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i))
}
return new Blob([new Uint8Array(array)], { type: type })
}
file = dataURLtoFile(canvas.toDataURL('image/png'), 'image/png')// 先转base64 再生成blob类文件对象
param.append('file', file)
console.log(file)
let config = {
headers: { 'Content-Type': 'multipart/form-data' }
}
that.editorAxios.post(host, param, config).then(res => {
console.log(res.data, 'resdata')
// let url = 'https://static.runjian.com/' + dir + filename + extName
let url = host + '/' + dir + filename + extName
console.log(url, 'url')
// 设置背景图
that.graph.drawBackground({
color: '#f5f5f5',
image: url,
// position: { x: 0, y: 0 },
size: { width: that.graphOptionsWidth, height: that.graphOptionsHeight }
})
})
file图片文件绘制到可自定义宽高的canvas中
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 绘制图片 绘制有两种方法,drawPicture(矢量图) 和 drawBitmap(位图) drawPictur...
- 一、绘制图片 在自定义 View 时,难免会遇到难以绘制的图标和背景,这个时候,我们就需要用到绘制图片。在 and...
- 一、需求描述 如图,图1是其它游戏分享链接的显示效果(前提是在微信中打开并分享出去),图2是我们游戏页面(同样在微...
- 一、需求描述 如图,图1是其它游戏分享链接的显示效果(前提是在微信中打开并分享出去),图2是我们游戏页面(同样在微...