<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="atom.js"></script> <script src="libcanvas.js"></script> <script src="application.js"></script> </head> <body> <canvas></canvas> </body> </html>
*-full-compiled
versions from the repository.application.js
LibCanvas.extract(); atom.dom(function() { var canvas = atom.dom('canvas').first; var context = canvas.getContext('2d-libcanvas'); context .fillAll( '#efebe7' ) .fill( new Rectangle( 75, 75, 30, 30 ), 'green' ) .fill( new Circle ( 50, 50, 20 ) , '#c00' ); });
{ clear: null }
.addFunc( libcanvas.update )
. In fact, this is a bad decision, but in this case it is suitable. var libcanvas = new LibCanvas('canvas', { clear: null }); libcanvas.ctx.fillAll('black'); libcanvas .start(function () { this.ctx.fill( new Rectangle({ from: this.ctx.rectangle.getRandomPoint(), size: [ 2, 2 ] }), 'green' ); }) .addFunc( libcanvas.update );
var Item = atom.Class({ Implements: [ Drawable ], // - // Point - initialize: function (center, speed) { this.line = new Line( center, // // center.clone().move([ 20, 0 ]) ); // , // "/" this.speed = speed / 1000; }, update: function (time) { // this.line.to.rotate( // , fps // . fps , // , this.speed * time, this.line.from ); // libcanvas, // this.libcanvas.update(); }, draw: function () { // this.libcanvas.ctx .stroke( this.line, 'black' ); } }); var libcanvas = new LibCanvas('canvas'); // // . // . // var item = new Item( new Point(50, 50), (180).degree() ); libcanvas.addElement( item ) // "" LibCanvas, "start" .start() // , "item.update", // , bind : .addFunc( item.update.bind(item) );
shape
property, not the circle
. This is necessary for Draggable. var Item = atom.Class({ Implements: [ Drawable, Draggable /* */ ], [...] initialize: function (center, speed) { [...] // this.shape = new Circle( center, 25 ); }, [...] draw: function () { [...] .stroke( this.shape, '#c00' ); } }); [...] libcanvas.listenMouse(); item.draggable()
center
also moves, which is the beginning of the segment. The end of the segment remains in place and begins to turn on a new path. It must be shifted with the beginning. This is very easy to do by subscribing to the move event at a point: initialize: function (center, speed) { [...] // , center.addEvent('move', function (diff) { this.line.to.move(diff); }.bind(this)) },
var StopWatch = atom.Class({ Implements: [ Drawable, Draggable ], initialize: function (center) { this.center = center; this.millisec = this.line(); this.seconds = this.line(); this.minutes = this.line(); this.shape = new Circle( center, 25 ); center.addEvent('move', function (diff) { // "move" "diff" [this.millisec.to, this.seconds.to, this.minutes.to].invoke('move', diff); }.bind(this)); }, // line: function () { return new Line( this.center, this.center.clone().move([0, -20]) ); }, update: function (time) { var full = (360).degree(); // toSeconds, toMinutes toHours LibCanvas.Utils.Math // this.millisec.to.rotate( full * time.toSeconds(), this.center ); // - this.seconds .to.rotate( full * time.toMinutes(), this.center ); // - this.minutes .to.rotate( full * time.toHours() , this.center ); this.libcanvas.update(); }, draw: function () { this.libcanvas.ctx .stroke( this.shape , '#c00' ) .stroke( this.millisec, '#999' ) .stroke( this.seconds , '#000' ) .stroke( this.minutes , '#090' ); } });
Source: https://habr.com/ru/post/121047/
All Articles