On Github kellypacker / fabricjs-canvas-slides
Front-End Web Developer at
Oh hey, these are some notes. They'll be hidden in your presentation, but you can see them if you open the speaker notes window (hit 's' on your keyboard).Building an educational application for Pearson ERPI in Canada.
Canvas is a rectangle on the page where you can use Javascript to draw stuff.
<canvas id="myCanvas" width="300" height="200" style="border:1px solid black; background-color: orange;"></canvas>
<canvas id="myCanvas" width="300" height="200" style="border:1px solid black; background-color: orange;"></canvas>
var myCanvas = document.getElementById("myCanvas"); var ctx = myCanvas.getContext("2d"); // (x, y, width, height) ctx.fillRect(20, 30, 150, 75);
Fabric.js is a powerful and simple Javascript HTML5 canvas library.
Fabric provides interactive object model on top of canvas element.
<script src="js/fabric.min.js"></script>
<canvas id="myCanvas" width="300" height="200" style="border:1px solid black;"></canvas>
var canvas = new fabric.Element("myCanvas"); var rect = new fabric.Rect({ top: 100, left: 10, fill: "red", width: 180, height: 100 }); canvas.add(rect);
<canvas id="myCanvas" width="300" height="200"></canvas>
<div class="canvas-container"> <!-- Group selection --> <canvas id="myCanvas" width="250" height="300" class="lower-canvas"></canvas> <!-- Rendering --> <canvas class="upper-canvas" width="250" height="300"></canvas> </div>
This keeps group selection fast no matter how many objects are currently rendered on canvas.
rect.getWidth(); rect.getLeft(); rect.getTop(); rect.getFill(); // rgb(0,0,0) rect.getStroke(); rect.getOpacity(); // 1
var rect = new fabric.Rect(); rect.set({ width: 10, height: 20, fill: '#f55', opacity: 0.7 });
var text = new fabric.Text('hello world', { fontSize: 30 }); var circle = new fabric.Circle({ radius: 100, fill: '#eef', scaleY: 0.5 }); var group = new fabric.Group([ text, circle ], { left: 150, top: 100, angle: -10 }); canvas.add(group);
canvas.on('object:moving', function(e) { var activeObj = e.target; console.log(activeObj.get('left'), activeObj.get('top')); }); // attach an event to an object rect.on("mousedown", closeTool);
var LabeledRect = fabric.util.createClass(fabric.Rect, { type: 'labeledRect', initialize: function(options) { options || (options = { }); this.callSuper('initialize', options); this.set('label', options.label || ''); }, toObject: function() { return fabric.util.object.extend(this.callSuper('toObject'), { label: this.get('label') }); }, _render: function(ctx) { this.callSuper('_render', ctx); ctx.font = '20px Helvetica'; ctx.fillStyle = '#333'; ctx.fillText(this.label, -this.width/2, -this.height/2 + 20); } });
Benchmarks150 random objects: Initialization: 151ms Rendering: 79ms Total time: 230ms
canvas.renderOnAddRemove = false
2400+ tests
Unit, functional, import/export
If you are brave, there is some IE<9 support with excanvas
Canvas can be serialized to JSON or SVG and restored
node build.js modules=text,serialization,parser