From 8ee50f5d6401acf09fd90287c66469b5ac2b2f9b Mon Sep 17 00:00:00 2001 From: Robert Pataki Date: Sat, 25 Jun 2011 19:38:12 +0100 Subject: [PATCH] Square and rectangle shapes added. --- CanvasLoader.html | 47 ++++- js/CanvasLoader.js | 234 ++++++++++++++----------- shapeDrawingTests/rectangleTest.html | 75 -------- shapeDrawingTests/roundedRectTest.html | 74 -------- shapeDrawingTests/squareTest.html | 74 -------- 5 files changed, 172 insertions(+), 332 deletions(-) delete mode 100644 shapeDrawingTests/rectangleTest.html delete mode 100644 shapeDrawingTests/roundedRectTest.html delete mode 100644 shapeDrawingTests/squareTest.html diff --git a/CanvasLoader.html b/CanvasLoader.html index 6bb3379..0d2164e 100644 --- a/CanvasLoader.html +++ b/CanvasLoader.html @@ -40,19 +40,48 @@ + +
+
+
+
\ No newline at end of file diff --git a/js/CanvasLoader.js b/js/CanvasLoader.js index d5cf0c8..17b576d 100644 --- a/js/CanvasLoader.js +++ b/js/CanvasLoader.js @@ -38,18 +38,14 @@ * @class CanvasLoader * @constructor * @param {String} id - * @param {String} diameter + * @param {String} _diameter * @param {String} color **/ CanvasLoader = function(options) { this.initialize(options); }; // Define the shapes - var CIRCLE = "circle"; - var SQUARE = "square"; - var RECTANGLE = "rectangle"; - var TRIANGLE = "triangle"; - var shapes = [CIRCLE, SQUARE, TRIANGLE, RECTANGLE]; + var shapes = ["circle", "square", "rectangle", "roundedRectangle"]; var p = CanvasLoader.prototype; @@ -87,23 +83,23 @@ this._context = this._canvas.getContext("2d"); this._canvas.id = "CanvasLoader"; this._container.appendChild(this._canvas); - this._canvas.width = this._canvas.height = this.diameter; + this._canvas.width = this._canvas.height = this._diameter; // Create the cache canvas element this._cacheCanvas = document.createElement("canvas"); document.body.appendChild(this._cacheCanvas); this._cacheContext = this._cacheCanvas.getContext("2d"); - this._cacheCanvas.width = this._cacheCanvas.height = this.diameter; + this._cacheCanvas.width = this._cacheCanvas.height = this._diameter; this._cacheCanvas.style.display = "none"; // Set the RGB color object - this.setColor(this.color); + this.setColor(this._color); // Set the instance ready p._ready = true; // Draw the shapes on the canvas - this.draw(); + this._draw(); }; ///////////////////////////////////////////////////////////////////////////////////////////// @@ -173,13 +169,13 @@ p._activeId = 0; /** - * The diameter of the loader. - * @property diameter + * The _diameter of the loader. + * @property _diameter * @type Number **/ - p.diameter = 50; - p.setDiameter = function(diameter) { if(!isNaN(diameter)) this.diameter = Math.round(Math.abs(diameter)); this.redraw(); }; - p.getDiameter = function() { return this.diameter; }; + p._diameter = 50; + p.setDiameter = function(diameter) { if(!isNaN(diameter)) this._diameter = Math.round(Math.abs(diameter)); this._redraw(); }; + p.getDiameter = function() { return this._diameter; }; /** * The color of the loader shapes in RGB. @@ -190,83 +186,83 @@ /** * The color of the loader shapes in HEX. - * @property color + * @property _color * @type String **/ - p.color = "#000000"; - var colorReg = /^#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/; - p.setColor = function(color) { this.color = colorReg.test(color) ? color : "#000000"; this._colorRGB = this.getRGB(this.color); this.redraw(); }; - p.getColor = function() { return this.color; }; + p._color = "#000000"; + var colorReg = /^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/; + p.setColor = function(color) { this._color = colorReg.test(color) ? color : "#000000"; this._colorRGB = this._getRGB(this._color); this._redraw(); }; + p.getColor = function() { return this._color; }; /** * The type of the loader shapes. - * @property shape + * @property _shape * @type String **/ - p.shape = CIRCLE; + p._shape = shapes[2]; p.setShape = function(shape) { var i = 0; while(i < shapes.length) { - if(shape == shapes[i]) { this.shape = shape; this.redraw(); break; } + if(shape == shapes[i]) { this._shape = shape; this._redraw(); break; } ++i; } }; - p.getShape = function() { return this.shape; }; + p.getShape = function() { return this._shape; }; /** * The number of shapes drawn on the loader canvas. - * @property density + * @property _density * @type Number **/ - p.density = 120; - p.setDensity = function(density) { if(!isNaN(density)) this.density = Math.round(Math.abs(density)); this.redraw(); }; - p.getDensity = function() { return this.density; }; + p._density = 12; + p.setDensity = function(density) { if(!isNaN(density)) this._density = Math.round(Math.abs(density)); this._redraw(); }; + p.getDensity = function() { return this._density; }; /** * The range of the animated loader shapes. - * @property range + * @property _range * @type Number **/ - p.range = 0.95; - p.setRange = function(range) {if(!isNaN(range)) this.range = Math.abs(range); this.redraw(); }; - p.getRange = function() { return this.range; }; + p._range = 0.95; + p.setRange = function(range) {if(!isNaN(range)) this._range = Math.abs(range); this._redraw(); }; + p.getRange = function() { return this._range; }; /** * The scaling of the loader shapes. - * @property scaling + * @property _scaling * @type Boolean **/ - p.scaling = true; - p.setScaling = function(scaling) { if(typeof(scaling) == "boolean") this.scaling = scaling; this.redraw(); }; - p.getScaling = function() { return this.scaling; }; + p._scaling = true; + p.setScaling = function(scaling) { if(typeof(scaling) == "boolean") this._scaling = scaling; this._redraw(); }; + p.getScaling = function() { return this._scaling; }; /** * The fading of the loader shapes. - * @property fading + * @property _fading * @type Boolean **/ - p.fading = true; - p.setFading = function(fading) { if(typeof(fading) == "boolean") this.fading = fading; this.redraw(); }; - p.getFading = function() { return this.fading; }; + p._fading = true; + p.setFading = function(fading) { if(typeof(fading) == "boolean") this._fading = fading; this._redraw(); }; + p.getFading = function() { return this._fading; }; /** * The speed of the loader animation. * @property _speed * @type Number **/ - p.speed = 1; - p.setSpeed = function(speed) {if(!isNaN(speed) && Math.abs(speed) > 0) this.speed = Math.round(Math.abs(speed)); this.reset();}; - p.getSpeed = function() { return this.speed; }; + p._speed = 1; + p.setSpeed = function(speed) {if(!isNaN(speed) && Math.abs(speed) > 0) this._speed = Math.round(Math.abs(speed)); this._reset();}; + p.getSpeed = function() { return this._speed; }; /** * The FPS of the loader animation rendering. - * @property fps + * @property _fps * @type Number **/ - p.fps = 12; + p._fps = 2; // [GS]etter for the FPS - p.getFPS = function() { return this.fps }; - p.setFPS = function(fps) { if(!isNaN(fps)) this.fps = Math.round(Math.abs(fps)); this.reset();}; + p.getFPS = function() { return this._fps }; + p.setFPS = function(fps) { if(!isNaN(fps)) this._fps = Math.round(Math.abs(fps)); this._reset();}; // End of Property declarations ///////////////////////////////////////////////////////////////////////////////////////////// @@ -274,7 +270,7 @@ /** * Return the RGB values of the passed color. */ - p.getRGB = function(color) { + p._getRGB = function(color) { var hexObject = {}; color = color.charAt(0) == "#" ? color.substring(1, 7) : color; @@ -289,65 +285,100 @@ /** * Draw the shapes on the canvas */ - p.draw = function() { + p._draw = function() { var i = 0; - var size = this.diameter * .07; + var size = this._diameter * .07; var radians; - var x, y, a; - var animBits = Math.round(this.density * this.range); - var minBitMod = 1/animBits; + var x, y, a, angle, minBitMod; + var animBits = Math.round(this._density * this._range); var bitMod; // Clean the cache canvas this._cacheContext.clearRect(0, 0, this._cacheCanvas.width, this._cacheCanvas.height); - this._canvas.width = this._canvas.height = this._cacheCanvas.width = this._cacheCanvas.height = this.diameter; + this._canvas.width = this._canvas.height = this._cacheCanvas.width = this._cacheCanvas.height = this._diameter; // Draw the shapes - switch(this.shape) + switch(this._shape) { - case CIRCLE: - while(i 360 ? this.density / 360 : 360 / this.density; - rotUnit *= this.speed; + p._tick = function(initialize) { + var rotUnit = this._density > 360 ? this._density / 360 : 360 / this._density; + rotUnit *= this._speed; if(!initialize) this._activeId += rotUnit; if(this._activeId > 360) this._activeId -= 360; - this._context.clearRect(0, 0, this.diameter, this.diameter); + this._context.clearRect(0, 0, this._diameter, this._diameter); this._context.save(); - this._context.translate(this.diameter*0.5, this.diameter*0.5); + this._context.translate(this._diameter*0.5, this._diameter*0.5); this._context.rotate(Math.PI/180*this._activeId); - this._context.translate(-this.diameter*0.5, -this.diameter*0.5); - this._context.drawImage(this._cacheCanvas, 0, 0, this.diameter, this.diameter); + this._context.translate(-this._diameter*0.5, -this._diameter*0.5); + this._context.drawImage(this._cacheCanvas, 0, 0, this._diameter, this._diameter); this._context.restore(); }; @@ -415,7 +441,7 @@ if(!this._running) { this._running = true; var t = this; - this._timer = self.setInterval(function(){t.tick();}, Math.round(1000/this.fps)); + this._timer = self.setInterval(function(){t._tick();}, Math.round(1000/this._fps)); } }; @@ -432,6 +458,14 @@ } }; + /** + * Remove the CanvasLoader instance + */ + p.remove = function() { + if(this._running) this.stop(); + this._container.removeChild(this._canvas); + }; + window.CanvasLoader = CanvasLoader; document.CanvasLoader = CanvasLoader; diff --git a/shapeDrawingTests/rectangleTest.html b/shapeDrawingTests/rectangleTest.html deleted file mode 100644 index c77d597..0000000 --- a/shapeDrawingTests/rectangleTest.html +++ /dev/null @@ -1,75 +0,0 @@ - - - - Canvas Test - Centered rotation - - - - - - - - - - \ No newline at end of file diff --git a/shapeDrawingTests/roundedRectTest.html b/shapeDrawingTests/roundedRectTest.html deleted file mode 100644 index e5c76d5..0000000 --- a/shapeDrawingTests/roundedRectTest.html +++ /dev/null @@ -1,74 +0,0 @@ - - - - Canvas Test - Centered rotation - - - - - - - - - - \ No newline at end of file diff --git a/shapeDrawingTests/squareTest.html b/shapeDrawingTests/squareTest.html deleted file mode 100644 index e5c76d5..0000000 --- a/shapeDrawingTests/squareTest.html +++ /dev/null @@ -1,74 +0,0 @@ - - - - Canvas Test - Centered rotation - - - - - - - - - - \ No newline at end of file