var circle = new LibCanvas.Shapes.Circle( 100, 100, 50 ); var circle = new LibCanvas.Shapes.Circle( 222, 222, 22 );
LibCanvas.extract(); var circle = new Circle( 100, 100, 50 ); var circle = new Circle( 222, 222, 22 );
var Circle = LibCanvas.Shapes.Circle; var circle1 = new Circle( 100, 100, 50 ); var circle2 = new Circle( 222, 222, 22 );
var context = document.getElementsByTagName('canvas')[0].getContext('2d-libcanvas'); // or: var context = atom.dom('canvas').first.getContext('2d-libcanvas');
var context = atom.dom('canvas').first.getContext('2d');
context .set({ fillStyle: 'black', strokeStyle: 'red' }) . fillRect(20, 20, 40, 40) .strokeRect(50, 50, 100, 100);
context.drawImage(img, 10, 15, 40, 45, 20, 25, 50, 55); // vs context.drawImage({ image: img, crop : [10, 15, 40, 45], draw : [20, 25, 50, 50] });
// : context.drawImage( image, rect.from.x, rect.from.y, rect.width, rect.height ); // vs context.drawImage( image, rect ); // : context.save(); context.fillStyle = 'red'; context.fillRect( rect.from.x, rect.from.y, rect.width, rect.height ) context.restore(); // vs: context.fill( rect, 'red' );
// , : // original ctx: context.save(); context.translate(this.position.x, this.position.y); context.rotate(this.angle); context.translate(-this.image.width/2, -this.image.height/2); context.drawImage(this.image, 0, 0); context.restore(); // vs context.drawImage({ image : this.image, center: this.position, angle : this.angle }); // : context.text({ text: 'Test string \n with line breaks \n is here' padding: [ 30, 50 ], size: 20, align: 'center' }) // : context.translate( point.x, point.y); context.rotate(angle); context.translate(-point.x, -point.y); // vs: context.rotate( angle, point ); // context.beginPath( ); context.moveTo( mt.x, mt.y ); context.lineTo( lt.x, lt.y ); context.bezierCurveTo( bc1.x, bc1.y, bc2.x, bc2.y, bc.x, bc.y ); context.quadraticCurveTo( qc1.x, qc1.y, qc.x, qc.y ); context.closePath(); // vs context .beginPath( mt ) .lineTo( lt ); .curveTo( bc, bc1, bc2 ) .curveTo( qc, qc1 ) .closePath(); // : var circle = new Circle( 130, 120, 50 ); context.beginPath(); context.arc( circle.center.x, circle.center.y, circle.radius, 0, Math.PI * 2 ); context.closePath(); context.clip(); // vs: context.clip( circle ); // : context.clear( 0, 0, canvas.width, canvas.height ); // vs context.clearAll();
var libcanvas = new LibCanvas('#my-canvas'); libcanvas instanceof LibCanvas; // true libcanvas instanceof LibCanvas.Canvas2D; // true // : libcanvas.ctx instanceof LibCanvas.Context2D; // true
libcanvas.update()
method.libcanvas.addRender()
method, you can add a function to the render phase using the libcanvas.addRender()
method. Also, at the render stage, the draw methods of the transferred objects are invoked. Approximately the code looks like this: libcanvas .addFunc(function () { scene.recount(); if (scene.somethingChanged()) { libcanvas.update(); } }) .addRender(function () { // libcanvas.update(); scene.drawAll(); });
addRender
is rarely used, since It is very convenient to draw objects using the draw()
method (see below).LibCanvas.Point
is one of the basic objects. It is used very often, is a component of all the figures and is very convenient to use outside of them . It has methods for determining the distance between two points, the angle, the point multiplication, and also getting all the neighbors. // A 60 B: var A = new Point(10, 10), B = new Point(20, 20); A.rotate( (60).degree(), B ); // : var sum = 0 + matrix[py-1][px-1] + matrix[py-1][px] + matrix[py-1][p.x+1] + matrix[py ][px-1] + matrix[py ][p.x+1] + matrix[p.y+1][px-1] + matrix[p.y+1][px] + matrix[p.y+1][p.x+1] ; // vs var sum = point.neighbours.reduce(function(value, p) { return value + matrix[py][px]; }, 0);
LibCanvas.Shapes.*
And globalize to short aliases. The most famous shapes are Rectangle
, Circle
, Line
. When using LibCanvas, you must realize that the figures themselves do not have an appearance, they cannot have an appearance — color or shadow. An object that uses a shape, such as LibCanvas.Ui.Shaper, is responsible for the appearance, while the figures themselves contain only mathematical operations - how to pass the path, intersections, whether the point is inside the shape, etc. They are the astral, but not the physical body. var Unit = atom.Class({ initialize: function (rectangle, image) { this.shape = rectangle; this.image = image; }, collision: function (anotherUnit) { return this.shape.intersect( anotherUnit.shape ); }, draw: function () { this.libcanvas.ctx.drawImage( this.image, this.shape ); } });
Rectangle
is the most important shape. It is used not only during drawing rectangles and basic mathematical operations, but also in many methods of LibCanvas. This could be, for example, the context.drawImage method, which takes arguments to cut and draw a rectangle or a tile engine, in which each element is a small Rectangle. context.drawImage({ image: image, crop: { from: { x: 15, y: 10 }, size: { width: 50, height: 100 } }, draw: [10,20,100,200] });
var Item = atom.Class({ initialize: function (image) { this.image = image; this.cropRect = new Rectangle(15, 10, 50, 100); this.drawRect = new Rectangle(10, 20, 100, 200); }, draw: function () { context.drawImage({ image: this.image, crop : this.cropRect, draw : this.drawRect }); } });
// : context.arc({ circle: new Circle( 100, 100, 50 ), angle : [ (45).degree(), (135).degree() ] }); // : context.stroke( new Line([13, 13], [42, 42]), 'red' );
LibCanvas.Behaviors.*
. Each of them is just an admixture that adds to your class a certain functionality or behavior. For example, Animatable
adds the animate
method which allows you to change the properties of an object smoothly, and Drawable
allows objects of your class to be added to the LibCanvas object for drawing. var Item = atom.Class({ Implements: [ Drawable, Draggable ], initialize: function (shape) { this.shape = shape; }, draw: function () { this.libcanvas.ctx.stroke( this.shape, 'red' ); } }); libcanvas.addElement( new Item( new Rectangle(50, 50, 100, 100) ).draggable() );
Ui.Shaper
already implemented: libcanvas.createShaper({ shape : new Rectangle(50, 50, 100, 100), stroke: 'red' }).draggable();
libcanvas.listenKeyboard()
method and you can use the libcanvas.getKey( keyName )
method, if necessary, to find out the state of the key: update: function () { if( this.libcanvas.getKey('aup') ) { this.move(); } }
libcanvas.listenMouse()
method. In order to optimize, mouse events are not analyzed before it is called, because there are applications that do not need a mouse. After that, you can easily subscribe to mouse events by adding an element to the Mouse object: this.libcanvas.mouse.subscribe( element );
LibCanvas.Shapes.*
), The zIndex property is and it has implemented the class atom.Class.Events
. In practice, all this is hidden behind the behaviors and when you call, for example, the draggable()
method of the Draggable
behavior, the object automatically subscribes to mouse events. If you only need to listen to mouse events, then it is sufficient to implement the MouseListener
behavior and call the listenMouse
method. However, the most important thing is still the element - the element must have the Shape
property with some figure inside. When mouse events on your object are being listened to, you can subscribe to any of the following events: /* - click - mouseover - mousemove - mouseout - mouseup - mousedown - away:mouseover - away:mousemove - away:mouseout - away:mouseup - away:mousedown */ // : element .listenMouse() .addEvent('click', function () { alert('element clicked'); });
Source: https://habr.com/ru/post/121046/
All Articles