fillRect(left, top, width, height)
. Draw a line? Use the moveTo(left, top)
and lineTo(x, y)
combination. It’s as if we paint with a brush on a canvas , putting more and more paint, almost without any control.
// canvas (id="c") var canvasEl = document.getElementById('c'); // 2d , ("bitmap" ) var ctx = canvasEl.getContext('2d'); // fill () ctx.fillStyle = 'red'; // 100,100 20x20 ctx.fillRect(100, 100, 20, 20);
// "" canvas (id="c") var canvas = new fabric.Canvas('c'); // var rect = new fabric.Rect({ left: 100, top: 100, fill: 'red', width: 20, height: 20 }); // , canvas.add(rect);
var canvasEl = document.getElementById('c'); var ctx = canvasEl.getContext('2d'); ctx.fillStyle = 'red'; <b>ctx.translate(100, 100); ctx.rotate(Math.PI / 180 * 45); ctx.fillRect(-10, -10, 20, 20);</b>
var canvas = new fabric.Canvas('c'); // 45 var rect = new fabric.Rect({ left: 100, top: 100, fill: 'red', width: 20, height: 20, <b>angle: 45</b> }); canvas.add(rect);
45
. But with the usual methods, it's not so simple. First, we cannot manage objects directly. Instead, you have to change the position and angle of the bitmap itself ( ctx.translate
, ctx.rotate
). Then we draw a rectangle, while not forgetting to move the bitmap accordingly (-10, -10), so that the rectangle appears at 100,100. You also need to remember to convert the angle from degrees to radians when you turn the bitmap.
fillRect
again?
fillRect
command, the rectangle is drawn directly on top of the entire bitmap. That is why I brought an analog brush with paint. To move the figure, we need to first erase the previous result, and then draw in a new place.
var canvasEl = document.getElementById('c'); ... ctx.strokRect(100, 100, 20, 20); ... // canvas <b>ctx.clearRect(0, 0, canvasEl.width, canvasEl.height); ctx.fillRect(20, 50, 20, 20);</b>
var canvas = new fabric.Canvas('c'); ... canvas.add(rect); ... <b>rect.set({ left: 20, top: 50 }); canvas.renderAll();</b>
fabric.Rect
constructor. But, of course, Fabric provides many other simple shapes: circles, triangles, ellipses, etc. All of them are accessible from fabric
objects, respectively, fabric.Circle
, fabric.Triangle
, fabric.Ellipse
, etc.
var circle = new fabric.Circle({ radius: 20, fill: 'green', left: 100, top: 100 }); var triangle = new fabric.Triangle({ width: 20, height: 30, fill: 'blue', left: 50, top: 50 }); canvas.add(circle, triangle);
set
method moved the object to a new position set({ left: 20, top: 50 })
. Similarly, you can change any other attributes that are available several.
true
to the flip * attribute.
get
method, assignment is done using set
. Let's change our rectangle somehow.
var canvas = new fabric.Canvas('c'); ... canvas.add(rect); rect.set('fill', 'red'); rect.set({ strokeWidth: 5, stroke: 'rgba(100,200,200,0.5)' }); rect.set('angle', 15).set('flipY', true);
set()
is a fairly universal method. It is intended for frequent use, so it is sharpened for convenience.
get()
, as well as a set of specific get*()
methods. For example, to get the “width” of an object, you can use get('width')
or getWidth()
. For scaleX, get('scaleX')
or getScaleX()
, etc. Special methods such as getWidth()
and getScaleX()
exist for all the “public” attributes of the object (“stroke”, “strokeWidth”, “angle”, etc.)
set
method. This is because they are really the same. An object can be “configured” at the time of creation, or later, using the set
method. The syntax is absolutely the same:
var rect = new fabric.Rect({ width: 10, height: 20, fill: '#f55', opacity: 0.7 }); // var rect = new fabric.Rect(); rect.set({ width: 10, height: 20, fill: '#f55', opacity: 0.7 });
var rect = new fabric.Rect(); // rect.getWidth(); // 0 rect.getHeight(); // 0 rect.getLeft(); // 0 rect.getTop(); // 0 rect.getFill(); // rgb(0,0,0) rect.getStroke(); // null rect.getOpacity(); // 1
fabric.Object
. fabric.Object
is an abstract 2-dimensional figure on a plane. It has left / top and width / height attributes, as well as a set of other visual parameters. The attributes we saw earlier (fill, stroke, angle, opacity, flip *, etc.) belong to all Fabric objects that inherit from fabric.Object
.
fabric.Object
, thus making it available in all “classes” of descendants. For example, if you need a getAngleInRadians
method on all objects, simply create it on fabric.Object.prototype
:
fabric.Object.prototype.getAngleInRadians = function() { return this.getAngle() / 180 * Math.PI; }; var rect = new fabric.Rect({ angle: 45 }); rect.getAngleInRadians(); // 0.785... var circle = new fabric.Circle({ angle: 30, radius: 10 }); circle.getAngleInRadians(); // 0.523... circle instanceof fabric.Circle; // true circle instanceof fabric.Object; // true
fabric.Object
, but also define their own methods and parameters. For example, in fabric.Circle
there is an additional attribute “radius”. Or take for example fabric.Image
, with which we will learn more later. It contains getElement
/ setElement
methods for reading / writing HTML <img> element, on which the object of the type fabric.Image
.
new fabric.Canvas('...')
. fabric.Canvas is, in fact, a wrapper around the <canvas> element, responsible for managing all the objects it contains. The constructor takes the id of the element, and returns an object of type fabric.Canvas
.
add()
) objects to it, as well as read them ( item()
, getObjects()
), or remove()
( remove()
):
var canvas = new fabric.Canvas('c'); var rect = new fabric.Rect(); canvas.add(rect); // canvas.item(0); // fabric.Rect, ( ) canvas.getObjects(); // ( ) canvas.remove(rect); //
fabric.Canvas
is to manage the objects that are on it. Also, it can be configured through a set of parameters. Settings such as changing the background of the canvas, hiding objects by mask, changing the overall length / width, turning on / off interactivity - these and other options can be set directly to the fabric.Canvas
both during creation and later:
var canvas = new fabric.Canvas('c', { backgroundColor: 'rgb(100,100,200)', selectionColor: 'blue', selectionLineWidth: 2 // ... }); // var canvas = new fabric.Canvas('c'); canvas.backgroundImage = 'http://...'; canvas.onFpsUpdate = function(){ /* ... */ }; // ...
new fabric.Canvas('...')
, the objects located on it can immediately be selected, moved, scaled, rotated and even grouped together , controlling them as one!
var canvas = new fabric.Canvas('c'); ... canvas.selection = false; // rect.set('selectable', false); //
fabric.Canvas
to fabric.StaticCanvas
. The syntax (configuration, methods) is absolutely identical, just use the word StaticCanvas
instead of Canvas
.
var staticCanvas = new fabric.StaticCanvas('c'); staticCanvas.add( new fabric.Rect({ width: 10, height: 20, left: 100, top: 100, fill: 'yellow', angle: 30 }));
fabric.Image
object, add it to the canvas:
<canvas id="c"></canvas> <img src="my_image.png" id="my-image">
var canvas = new fabric.Canvas('c'); var imgElement = document.getElementById('my-img'); var imgInstance = new fabric.Image(imgElement, { left: 100, top: 100, angle: 30, opacity: 0.85 }); canvas.add(imgInstance);
fabric.Image
constructor. Thus, we create an object of the type fabric.Image
, which is a picture of this element. We also set the left / top values ​​to 100/100, the angle to 30, and the transparency to 0.85. Once added to the canvas, the picture is rendered at position 100,100, rotated 30 degrees, and slightly transparent! Not bad...
fabric.Image.fromURL
:
fabric.Image.fromURL('my_image.png', function(oImg) { canvas.add(oImg); });
fabric.Image.fromURL
passing the address of the image, as well as the function (callback), which should be called when the picture is loaded. The callback receives the fabric.Image
object fabric.Image
first argument. At the time of the call, you can do anything with it — change it, or immediately add it to the canvas for display.
fabric.Image.fromURL('my_image.png', function(oImg) { // oImg.scale(0.5).setFlipX(true); canvas.add(oImg); });
var canvas = new fabric.Canvas('c'); var path = new fabric.Path('M 0 0 L 200 100 L 170 200 z'); path.set({ left: 120, top: 120 }); canvas.add(path);
fabric.Path
object, we pass a string with instructions for “drawing” the curve. This instruction looks, of course, very mysterious, but to understand it is actually quite easy. “M” means “move”, and tells the invisible pen to move to point 0, 0. “L” means “line” (line) and draws a line to point 200, 100. Then the “L” command draws a line to 170 , 200. Finally, “z” causes the invisible handle to close the current path and complete the shape. As a result, this triangular shape is obtained.
fabric.Path
object is the same as the other objects in Fabric, so we easily changed its parameters (left, top). But you can change more:
... 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);
... var path = new fabric.Path('M121.32,0L44.58,0C36.67,0,29.5,3.22,24.31,8.41\ c-5.19,5.19-8.41,12.37-8.41,20.28c0,15.82,12.87,28.69,28.69,28.69c0,0,4.4,\ 0,7.48,0C36.66,72.78,8.4,101.04,8.4,101.04C2.98,106.45,0,113.66,0,121.32\ c0,7.66,2.98,14.87,8.4,20.29l0,0c5.42,5.42,12.62,8.4,20.28,8.4c7.66,0,14.87\ -2.98,20.29-8.4c0,0,28.26-28.25,43.66-43.66c0,3.08,0,7.48,0,7.48c0,15.82,\ 12.87,28.69,28.69,28.69c7.66,0,14.87-2.99,20.29-8.4c5.42-5.42,8.4-12.62,8.4\ -20.28l0-76.74c0-7.66-2.98-14.87-8.4-20.29C136.19,2.98,128.98,0,121.32,0z'); canvas.add(path.set({ left: 100, top: 200 }));
fabric.loadSVGFromString
or fabric.loadSVGFromURL
, which loads an entire SVG file. Everything else will be done by the Fabric parser, passing through all SVG elements and creating the corresponding Path objects.
fabric.PathGroup
objects). PathGroup is just a group of Path objects. Since fabric.PathGroup
inherits from fabric.Object
, such objects can be added to the canvas like any other Fabric objects. Of course, they can be controlled, like everything else.
Source: https://habr.com/ru/post/162367/