Fabric.js
是一个功能强大且简单的
HTML5
画布库,它给
canvas
上的元素提供了交互式对象模型。借助
Fabric.js
,你可以轻松地绘制各种图形线条图片,对它们进行拖拽、旋转、形变等操作,并且支持丰富的事件方法。
1 2 3 4 5 6 7 8 9
|
const circle = new fabric.Circle({ radius: 100, left: 10, top: 10, fill: 'yellow', strokeWidth: 2, stroke: "red" }) canvas.add(circle)
|
1 2 3 4 5 6 7 8 9 10
|
const rect = new fabric.Rect({ width: 200, height: 100, rx: 20, ry: 20, fill: '#FF4D4F', strokeWidth: 2, stroke: "#dedede" }) canvas.add(rect)
|
1 2 3 4 5 6 7 8
|
const triangle = new fabric.Triangle({ width: 200, height: 300, fill: '#FF4D4F', strokeWidth: 2, stroke: "#dedede" }) canvas.add(triangle)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
const canvas = new fabric.Canvas('c') const grid = 30 const lineStroke = '#C5C9CB' for (let i = 0; i < (canvas.width / grid); i++) { const lineX = new fabric.Line([ 0, i * grid, canvas.width, i * grid], { stroke: lineStroke, selectable: false, type: 'line' }) const lineY = new fabric.Line([ i * grid, 0, i * grid, canvas.width], { stroke: lineStroke, selectable: false, type: 'line' }) canvas.add(lineX) canvas.add(lineY) }
|
1 2 3 4 5 6 7 8 9 10 11 12
|
const text = new fabric.IText('blackstar', { top: 100, left: 100, fontFamily: 'Calibri', fontSize: 18, angle: 45, originX: 'center', originY: 'center', centeredRotation: true, fill: '#000' }) canvas.add(text)
|
1 2 3 4
|
fabric.Image.fromURL('https://s2.ax1x.com/2019/08/18/mlm0Jg.jpg', function(oImg) { oImg.scale(0.5) canvas.add(oImg) })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
canvas.on('object:moving', function(e) { console.log(e.target) })
canvas.on('object:scaling', function(e) { console.log(e.target) })
canvas.on('object:rotating', function (e) { console.log(e.target) })
canvas.on('object:modified', function(e) { console.log(e.target) })
canvas.on('mouse:down', function (e) { console.log(e.target) }) canvas.on('mouse:up', function (e) { console.log(e.target) })
|