15 Star 174 Fork 63

德育处主任/Fabric.js学习资料(中文教程)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.html 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
德育处主任 提交于 2022-01-20 14:50 +08:00 . 上传背景图 添加实现方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>上传图片</title>
</head>
<body>
<div>
<input type="file" name="file" id="upload" onchange="handleUpload()" />
<button onclick="saveCanvas()">保存</button>
</div>
<canvas id="canvas" width="600" height="600" style="border: 1px solid #ccc;"></canvas>
<script src="../../script/fabric.js"></script>
<script>
// 上传文件的DOM元素
const uploadEl = document.getElementById("upload")
// 画布
let canvas = null
// 初始化画布
function initCanvas() {
canvas = new fabric.Canvas('canvas')
}
// 上传文件事件
function handleUpload() {
// 上传文件列表的第一个文件
const file = uploadEl.files[0]
// 图片文件的地址
let imgPath = null
// 获取图片文件真实路径
// 由于浏览器安全策略,现在需要这么做了
if (window.createObjcectURL != undefined) {
imgPath = window.createOjcectURL(file);
} else if (window.URL != undefined) {
imgPath = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) {
imgPath = window.webkitURL.createObjectURL(file);
}
// 实现方式1
// 创建一个fabric的图片,在回调函数里添加到画布上
// fabric.Image.fromURL(
// imgPath, // 真实图片地址
// img => {
// // 将图片设置再画布上,然后重新渲染画布,图片就出来了。
// canvas.setBackgroundImage(
// img, // 要设置的图片
// canvas.renderAll.bind(canvas) // 重新渲染画布
// )
// }
// )
// 实现方式2
canvas.setBackgroundImage(
imgPath,
canvas.renderAll.bind(canvas)
)
}
// 保存画布
function saveCanvas() {
let data = canvas.toJSON()
console.log(data)
}
window.onload = function() {
initCanvas()
}
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/k21vin/fabricjs-demo.git
git@gitee.com:k21vin/fabricjs-demo.git
k21vin
fabricjs-demo
Fabric.js学习资料(中文教程)
master

搜索帮助