Fabric.js介紹
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
Fabric.js是什么Fabric.js能做什么為什么使用Fabric.js// 獲取canvas元素 var canvasEl = document.getElementById('c'); // 獲取上下文 var ctx = canvasEl.getContext('2d'); // 設(shè)置填充顏色 ctx.fillStyle = 'red'; // 在100,100點創(chuàng)建20x20的矩形 ctx.fillRect(100, 100, 20, 20); // 創(chuàng)建原生canvas元素的包裝類(‘c’canvas元素的id) var canvas = new fabric.Canvas('c'); // 創(chuàng)建一個矩形對象 var rect = new fabric.Rect({ left: 100, top: 100, fill: 'red', width: 20, height: 20 }); // 添加矩形到畫布上 canvas.add(rect); // 擦除之前的矩形(這里是擦除了整個canvas區(qū)域) ctx.clearRect(0, 0, canvasEl.width, canvasEl.height); // 重新繪制矩形 ctx.fillRect(20, 50, 20, 20); 聽起來是不是很蹩腳?是的,使用Canvas原生方法時,就像是個帶橡皮檫的筆刷,在畫布上繪制圖形時,是筆刷在畫布上移動,繪制出的圖形就是筆刷移動的整個痕跡,而想把繪制好的圖形移動到其他地方,只好用橡皮檫擦掉,然后在新位置重新繪制。 rect.set({ left: 20, top: 50 }); canvas.renderAll(); Canvas對象var canvas = new fabric.Canvas('...') var canvasElement = document.getElementById(canvasEle); var ctx = canvasElement.getContext("2d"); fabric.Canvas對象負(fù)責(zé)管理畫布上繪制的所有對象,可以將對象添加到fabric.Canvas對象,也可以從fabric.Canvas獲取或刪除對象。 var canvas = new fabric.Canvas('c'); var rect = new fabric.Rect(); canvas.add(rect); // 添加對象 canvas.item(0); ///獲取之前添加的fabric.Rect(第一個對象) canvas.getObjects(); ///獲取畫布上的所有對象(rect將是第一個也是唯一一個) canvas.remove(rect); ///刪除之前添加的對象刪除 var canvas = new fabric.Canvas('c', { backgroundColor: 'rgb(100,100,100,200)', selectionColor:'blue', selectLineWidth: 2 // ... }); // 或 var canvas = new fabric.Canvas('c'); canvas.setBackgroundImage('http://...'); canvas.onFpsupdate = function(){ /* ...... */ }; // ... canvas.renderAll(); fabric.Circle fabric.Ellipse fabric.Line fabric.Polygon fabric.Polyline fabric.Rect fabric.Triangle 創(chuàng)建var line = new fabric.Line([x1, y1, x2, y2], { strokeWidth: 2, //線寬 stroke: rgba(255,0,0,0.8), //線的顏色 selectable: false }); canvas.add(line); var circle = new fabric.Circle({ radius: 2, left: left, top: top, originX: 'center', originY: 'center', fill: rgba(0,200,0,0.8), strokeWidth: 1, stroke: rgba(255,0,0,0.8), selectable: false }); canvas.add(circle); 控制fabric.Object.prototype.originX = fabric.Object.prototype.originY = 'center' line.left = pointer.x; line.top = pointer.y; line.set('stroke', startColor); line.set('height', 20); Image對象fabric.Image.fromURL('my_image.png', function(oImg) { // scale image down, and flip it, before adding it onto canvas oImg.scale(0.5).set('flipX, true); canvas.add(oImg); }); Path對象var path = new fabric.Path('M 0 0 L 200 100 L 170 200 z'); path.set({ left: 120, top: 120 }); canvas.add(path); ... var path = new fabric.Path('M 0 0 L 300 100 L 200 300 z'); ... path.set({ fill: 'red', stroke: 'green', opacity: 0.5 }); canvas.add(path); Events事件var canvas = new fabric.Canvas('...'); canvas.on('mouse:down', function(options) { console.log(options.e.clientX, options.e.clientY); }); canvas.on('mouse:down', function(options) { if (options.target) { console.log('一個對象被點擊了!', options.target.type); } }); var rect = new fabric.Rect({ width: 100, height: 50, fill: 'green' }); rect.on('elected', function() { console.log('選擇了一個矩形'); }); var circle = new fabric.Circle({ radius: 75, fill: 'blue' }); circle.on('selected', function() { console.log('選擇了一個圓圈'); }); Groups組var circle = new fabric.Circle({ radius: 100, fill: '#eef', scaleY: 0.5, originX: 'center', originY: 'center' }); var text = new fabric.Text('hello world', { fontSize: 30, originX: 'center', originY: 'center' }); var group = new fabric.Group([ circle, text ], { left: 150, top: 100, angle: -10 }); canvas.add(group); group.item(0).set('fill', 'red'); group.item(1).set({ text: 'trololo', fill: 'white' }); Serialization序列化toObject, toJSONvar canvas = new fabric.Canvas('c'); JSON.stringify(canvas); // '{"objects":[], "background": "rgba(0,0,0,0,0)"}' canvas.add(new fabric.Circle({ left: 100, top: 100, radius: 50, fill: 'red' })); console.log(JSON.stringify(canvas)); '{"objects":[{"type":"rect","left":50,"top":50,"width":20,"height":20,"fill":"green","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null, "scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"selectable":true,"hasControls":true,"hasBorders":true,"hasRotatingPoint":false, "transparentCorners":true,"perPixelTargetFind":false,"rx":0,"ry":0},{"type":"circle","left":100,"top":100,"width":100,"height":100,"fill":"red", "overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1, "selectable":true,"hasControls":true,"hasBorders":true,"hasRotatingPoint":false,"transparentCorners":true,"perPixelTargetFind":false,"radius":50}],"background":"rgba(0, 0, 0, 0)"}' toSvgcanvas.add(new fabric.Rect({ left: 50, top: 50, height: 20, width: 20, fill: 'green' })); console.log(canvas.toSVG()); <?xml version="1.0" standalone="no" ?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="800" height="700" xml:space="preserve"> <desc>created with Fabric.js 0.9.21</desc> <rect x="-10" y="-10" rx="0" ry="0" width="20" height="20" style="stroke: none; stroke-width: 1; stroke-dasharray: ; fill: green; opacity: 1;" transform="translate(50 50)" /> </svg> Deserialization反序列化, SVG Parser SVG解析器var canvas = new fabric.Canvas(); canvas.loadfromJSON('{"objects":[{"type":"rect","left":50,"top":50,"width":20,"height":20,"fill":"green","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null, "scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"selectable":true,"hasControls":true,"hasBorders":true,"hasRotatingPoint":false,"transparentCorners":true, "perPixelTargetFind":false,"rx":0,"ry":0},{"type":"circle","left":100,"top":100,"width":100,"height":100,"fill":"red","overlayFill":null,"stroke":null,"strokeWidth":1, "strokeDashArray":null,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"selectable":true,"hasControls":true,"hasBorders":true, "hasRotatingPoint":false,"transparentCorners":true,"perPixelTargetFind":false,"radius":50}],"background":"rgba(0, 0, 0, 0)"}'); 下移:canvas.sendBackwards(obj) 上移:canvas.bringForward(obj) 置頂:canvas.bringToFront(obj) 置底:canvas.sendToBack(obj) 更多資源該文章在 2023/5/23 11:21:46 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |