mirror of
https://github.com/RustyRaptor/CanvasLoader.git
synced 2026-12-03 17:55:15 -07:00
Square and rectangle shapes added.
This commit is contained in:
1 parent
6b73f865b9
commit
8ee50f5d64
5 files changed
+172
-332
No files matched your search
+38
-9
@@ -40,19 +40,48 @@
|
||||
<meta name="description" content="" />
|
||||
<meta name="keywords" content="" />
|
||||
<script type="application/javascript" src="js/CanvasLoader.js"></script>
|
||||
<style type="text/css">
|
||||
div {float:left; display:table; padding:20px;}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="circle"></div>
|
||||
<div id="circle2"></div>
|
||||
<div id="square"></div>
|
||||
<div id="rectangle"></div>
|
||||
<script type="text/javascript">
|
||||
var circleLoader = new CanvasLoader();
|
||||
circleLoader.setDiameter(30);
|
||||
circleLoader.start();
|
||||
var cl = new CanvasLoader({id:"circle"});
|
||||
cl.setShape("circle");
|
||||
cl.setDensity(60);
|
||||
cl.setFPS(12);
|
||||
cl.setSpeed(2);
|
||||
cl.setColor("#0099ff");
|
||||
cl.start();
|
||||
|
||||
var rectLoader = new CanvasLoader();
|
||||
rectLoader.setShape("rectangle");
|
||||
rectLoader.setDensity(12);
|
||||
rectLoader.setScaling(false);
|
||||
rectLoader.setFading(false);
|
||||
rectLoader.start();
|
||||
var cl1 = new CanvasLoader({id:"circle2"});
|
||||
cl1.setShape("circle");
|
||||
cl1.setDensity(60);
|
||||
cl1.setFPS(24);
|
||||
cl1.setSpeed(1);
|
||||
cl1.setScaling(false);
|
||||
cl1.setColor("#DA20DB");
|
||||
cl1.start();
|
||||
|
||||
var cl2 = new CanvasLoader({id:"square"});
|
||||
cl2.setShape("square");
|
||||
cl2.setDensity(12);
|
||||
cl2.setFPS(6);
|
||||
cl2.setSpeed(1);
|
||||
cl2.setColor("#B7C40D");
|
||||
cl2.start();
|
||||
|
||||
var cl3 = new CanvasLoader({id:"rectangle"});
|
||||
cl3.setShape("rectangle");
|
||||
cl3.setDensity(12);
|
||||
cl3.setFPS(12);
|
||||
cl3.setSpeed(1);
|
||||
cl3.setColor("#DE8E1A");
|
||||
cl3.start();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
+134
-100
@@ -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<this.density)
|
||||
case shapes[0]:
|
||||
minBitMod = 0.1;
|
||||
while(i<this._density)
|
||||
{
|
||||
if(i <= animBits) bitMod = 1-i*minBitMod;
|
||||
if(i <= animBits) bitMod = 1-((1-minBitMod)/animBits*i);
|
||||
else bitMod = minBitMod;
|
||||
|
||||
radians = (this.density - i) * ((Math.PI * 2) / this.density);
|
||||
x = this._canvas.width*.5 + Math.cos(radians) * (this.diameter*.45 - size) - this._canvas.width*.5;
|
||||
y = this._canvas.height*.5 + Math.sin(radians) * (this.diameter*.45 - size) - this._canvas.height*.5;
|
||||
radians = (this._density - i) * ((Math.PI * 2) / this._density);
|
||||
x = this._canvas.width*.5 + Math.cos(radians) * (this._diameter*.45 - size) - this._canvas.width*.5;
|
||||
y = this._canvas.height*.5 + Math.sin(radians) * (this._diameter*.45 - size) - this._canvas.height*.5;
|
||||
|
||||
this._cacheContext.beginPath();
|
||||
if(this.fading) this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + "," + bitMod + ")";
|
||||
if(this._fading) this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + "," + bitMod + ")";
|
||||
else this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + ",1)";
|
||||
if(this.scaling) this._cacheContext.arc(this.diameter*0.5 + x,this.diameter*0.5 + y,size*bitMod,0,Math.PI*2,false);
|
||||
else this._cacheContext.arc(this.diameter*0.5 + x,this.diameter*0.5 + y,size,0,Math.PI*2,false);
|
||||
if(this._scaling) this._cacheContext.arc(this._diameter*0.5 + x,this._diameter*0.5 + y,size*bitMod,0,Math.PI*2,false);
|
||||
else this._cacheContext.arc(this._diameter*0.5 + x,this._diameter*0.5 + y,size,0,Math.PI*2,false);
|
||||
this._cacheContext.closePath();
|
||||
this._cacheContext.fill();
|
||||
|
||||
++i;
|
||||
}
|
||||
break;
|
||||
case RECTANGLE:
|
||||
while(i<this.density)
|
||||
case shapes[1]:
|
||||
size = this._canvas.width * .12;
|
||||
minBitMod = 0.1;
|
||||
while(i<this._density)
|
||||
{
|
||||
if(i <= animBits) bitMod = 1-i*minBitMod;
|
||||
|
||||
radians = (this.density - i) * ((Math.PI * 2) / this.density);
|
||||
x = this._canvas.width*.5 + Math.cos(radians) * (this.diameter*.45 - size) - this._canvas.width*.5;
|
||||
y = this._canvas.height*.5 + Math.sin(radians) * (this.diameter*.45 - size) - this._canvas.height*.5;
|
||||
if(i <= animBits) bitMod = 1-((1-minBitMod)/animBits*i);
|
||||
else bitMod = minBitMod;
|
||||
angle = 360-360/this._density*i;
|
||||
radians = (angle)/180*Math.PI;
|
||||
x = Math.cos(radians) * size*3 + this._cacheCanvas.width*0.5;
|
||||
y = Math.sin(radians) * size*3 + this._cacheCanvas.height*0.5;
|
||||
|
||||
this._cacheContext.save();
|
||||
this._cacheContext.translate(x, y);
|
||||
this._cacheContext.rotate(radians);
|
||||
this._cacheContext.translate(-x, -y);
|
||||
this._cacheContext.beginPath();
|
||||
if(this.fading) this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + "," + bitMod + ")";
|
||||
if(this._fading) this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + "," + bitMod + ")";
|
||||
else this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + ",1)";
|
||||
|
||||
this._cacheContext.translate(this.diameter*0.5, this.diameter*0.5);
|
||||
this._cacheContext.rotate(Math.PI/180*i);
|
||||
this._cacheContext.translate(-this.diameter*0.5, -this.diameter*0.5);
|
||||
|
||||
if(this.scaling) this._cacheContext.fillRect(this.diameter*0.5 + x,this.diameter*0.5 + y, size*bitMod, size*bitMod*2);
|
||||
else this._cacheContext.fillRect(this.diameter*0.5 + x,this.diameter*0.5 + y,size,size*2);
|
||||
|
||||
|
||||
this._cacheContext.fillRect(x,y-size*.5,size,size);
|
||||
this._cacheContext.closePath();
|
||||
this._cacheContext.fill();
|
||||
this._cacheContext.restore();
|
||||
|
||||
++i;
|
||||
}
|
||||
break;
|
||||
case shapes[2]:
|
||||
var w = this._cacheCanvas.width * .2;
|
||||
var h = w*0.3;
|
||||
minBitMod = 0.1;
|
||||
while(i<this._density)
|
||||
{
|
||||
if(i <= animBits) bitMod = 1-((1-minBitMod)/animBits*i);
|
||||
else bitMod = minBitMod;
|
||||
angle = 360-360/this._density*i;
|
||||
radians = (angle)/180*Math.PI;
|
||||
if(this._scaling) {
|
||||
//w *= bitMod;
|
||||
x = Math.cos(radians) * (h + (this._cacheCanvas.height - h) * 0.2) + this._cacheCanvas.width*.5;
|
||||
y = Math.sin(radians) * (h + (this._cacheCanvas.height - h) * 0.2) + this._cacheCanvas.height*.5;
|
||||
}
|
||||
else {
|
||||
x = Math.cos(radians) * (h + (this._cacheCanvas.height - h) * 0.2) + this._cacheCanvas.width*.5
|
||||
y = Math.sin(radians) * (h + (this._cacheCanvas.height - h) * 0.2) + this._cacheCanvas.height*.5;
|
||||
}
|
||||
this._cacheContext.save();
|
||||
this._cacheContext.translate(x, y);
|
||||
this._cacheContext.rotate(radians);
|
||||
this._cacheContext.translate(-x, -y);
|
||||
this._cacheContext.beginPath();
|
||||
if(this._fading) this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + "," + bitMod + ")";
|
||||
else this._cacheContext.fillStyle = "rgba(" + this._colorRGB.r + "," + this._colorRGB.g + "," + this._colorRGB.b + ",1)";
|
||||
this._cacheContext.fillRect(x,y-h*.5,w,h);
|
||||
this._cacheContext.closePath();
|
||||
this._cacheContext.fill();
|
||||
this._cacheContext.restore();
|
||||
|
||||
++i;
|
||||
}
|
||||
@@ -355,56 +386,51 @@
|
||||
}
|
||||
|
||||
// Render the changes on the canvas
|
||||
this.tick(true);
|
||||
this._tick(true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Clean the canvas
|
||||
*/
|
||||
p.clean = function() {
|
||||
p._clean = function() {
|
||||
this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
|
||||
};
|
||||
|
||||
/**
|
||||
* Redraw the loader
|
||||
*/
|
||||
p.redraw = function() {
|
||||
p._redraw = function() {
|
||||
if(this._ready) {
|
||||
this.clean();
|
||||
this.draw();
|
||||
this._clean();
|
||||
this._draw();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Reset the timer
|
||||
*/
|
||||
p.reset = function() {
|
||||
p._reset = function() {
|
||||
if(this._running) {
|
||||
this.stop();
|
||||
this.start();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove the CanvasLoader instance
|
||||
*/
|
||||
p.remove = function() {};
|
||||
|
||||
/**
|
||||
* Render the loader
|
||||
*/
|
||||
p.tick = function(initialize) {
|
||||
var rotUnit = this.density > 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;
|
||||
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Canvas Test - Centered rotation</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="language" content="en" />
|
||||
<meta name="description" content="" />
|
||||
<meta name="keywords" content="" />
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="canvas" width="300" height="300" style="border:1px solid #ff0000;"></canvas>
|
||||
<script type="text/javascript">
|
||||
var canvas = document.getElementById("canvas");
|
||||
var context = canvas.getContext("2d");
|
||||
var density = 12;
|
||||
context.fillStyle = "rgba(255, 0, 0, 0.2)";
|
||||
|
||||
// Rotate the graphics
|
||||
function drawGraphics() {
|
||||
|
||||
context.clearRect(0, 0, context.width, context.height);
|
||||
|
||||
var i = 0;
|
||||
var w = canvas.width * .4;
|
||||
var h = w*0.3;
|
||||
var radians;
|
||||
var x, y, angle;
|
||||
while(i<this.density)
|
||||
{
|
||||
angle = 360/density*i;
|
||||
radians = (angle)/180*Math.PI;
|
||||
x = Math.cos(radians) * (h + (canvas.height - h) * 0.2) + canvas.width*.5
|
||||
y = Math.sin(radians) * (h + (canvas.height - h) * 0.2) + canvas.height*.5;
|
||||
|
||||
context.save();
|
||||
context.translate(x, y);
|
||||
context.rotate(radians);
|
||||
context.translate(-x, -y);
|
||||
context.beginPath();
|
||||
context.fillRect(x,y-h*.5,w,h);
|
||||
context.closePath();
|
||||
context.fill();
|
||||
context.restore();
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
//context.fillRect(5, 5, canvas.width-10, canvas.height-10);
|
||||
};
|
||||
|
||||
drawGraphics();
|
||||
|
||||
// Rotate the graphics
|
||||
function rotateGraphics(percent) {
|
||||
var radians = (360*percent)/180*Math.PI;
|
||||
var x = Math.round(canvas.width*.5 + Math.cos(radians) * (canvas.width*.45) - canvas.height*.5);
|
||||
var y = Math.round(canvas.height*.5 + Math.sin(radians) * (canvas.height*.45) - canvas.height*.5);
|
||||
context.save();
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
context.translate(canvas.width*0.5, canvas.height*0.5);
|
||||
context.rotate(radians);
|
||||
context.translate(-canvas.width*0.5, -canvas.height*0.5);
|
||||
drawGraphics();
|
||||
context.restore();
|
||||
};
|
||||
|
||||
// Handle the mouse move event
|
||||
canvas.onmousemove = function(event){
|
||||
var offsetX = 8;
|
||||
var percent = Math.floor((event.x - offsetX) / canvas.width * 100) / 100;
|
||||
rotateGraphics(percent);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,74 +0,0 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Canvas Test - Centered rotation</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="language" content="en" />
|
||||
<meta name="description" content="" />
|
||||
<meta name="keywords" content="" />
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="canvas" width="100" height="100" style="border:1px solid #ff0000;"></canvas>
|
||||
<script type="text/javascript">
|
||||
var canvas = document.getElementById("canvas");
|
||||
var context = canvas.getContext("2d");
|
||||
var density = 12;
|
||||
context.fillStyle = "rgba(255, 0, 0, 0.2)";
|
||||
|
||||
// Rotate the graphics
|
||||
function drawGraphics() {
|
||||
|
||||
context.clearRect(0, 0, context.width, context.height);
|
||||
|
||||
var i = 0;
|
||||
var size = canvas.width * .12;
|
||||
var radians;
|
||||
var x, y, angle;
|
||||
while(i<this.density)
|
||||
{
|
||||
angle = 360/density*i;
|
||||
radians = (angle)/180*Math.PI;
|
||||
x = Math.cos(radians) * size*3 + canvas.width*0.5;
|
||||
y = Math.sin(radians) * size*3 + canvas.height*0.5;
|
||||
|
||||
context.save();
|
||||
context.translate(x, y);
|
||||
context.rotate(radians);
|
||||
context.translate(-x, -y);
|
||||
context.beginPath();
|
||||
context.fillRect(x,y-size*.5,size,size);
|
||||
context.closePath();
|
||||
context.fill();
|
||||
context.restore();
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
//context.fillRect(5, 5, canvas.width-10, canvas.height-10);
|
||||
};
|
||||
|
||||
drawGraphics();
|
||||
|
||||
// Rotate the graphics
|
||||
function rotateGraphics(percent) {
|
||||
var radians = (360*percent)/180*Math.PI;
|
||||
var x = Math.round(canvas.width*.5 + Math.cos(radians) * (canvas.width*.45) - canvas.height*.5);
|
||||
var y = Math.round(canvas.height*.5 + Math.sin(radians) * (canvas.height*.45) - canvas.height*.5);
|
||||
context.save();
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
context.translate(canvas.width*0.5, canvas.height*0.5);
|
||||
context.rotate(radians);
|
||||
context.translate(-canvas.width*0.5, -canvas.height*0.5);
|
||||
drawGraphics();
|
||||
context.restore();
|
||||
};
|
||||
|
||||
// Handle the mouse move event
|
||||
canvas.onmousemove = function(event){
|
||||
var offsetX = 8;
|
||||
var percent = Math.floor((event.x - offsetX) / canvas.width * 100) / 100;
|
||||
rotateGraphics(percent);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,74 +0,0 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Canvas Test - Centered rotation</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta name="language" content="en" />
|
||||
<meta name="description" content="" />
|
||||
<meta name="keywords" content="" />
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="canvas" width="100" height="100" style="border:1px solid #ff0000;"></canvas>
|
||||
<script type="text/javascript">
|
||||
var canvas = document.getElementById("canvas");
|
||||
var context = canvas.getContext("2d");
|
||||
var density = 12;
|
||||
context.fillStyle = "rgba(255, 0, 0, 0.2)";
|
||||
|
||||
// Rotate the graphics
|
||||
function drawGraphics() {
|
||||
|
||||
context.clearRect(0, 0, context.width, context.height);
|
||||
|
||||
var i = 0;
|
||||
var size = canvas.width * .12;
|
||||
var radians;
|
||||
var x, y, angle;
|
||||
while(i<this.density)
|
||||
{
|
||||
angle = 360/density*i;
|
||||
radians = (angle)/180*Math.PI;
|
||||
x = Math.cos(radians) * size*3 + canvas.width*0.5;
|
||||
y = Math.sin(radians) * size*3 + canvas.height*0.5;
|
||||
|
||||
context.save();
|
||||
context.translate(x, y);
|
||||
context.rotate(radians);
|
||||
context.translate(-x, -y);
|
||||
context.beginPath();
|
||||
context.fillRect(x,y-size*.5,size,size);
|
||||
context.closePath();
|
||||
context.fill();
|
||||
context.restore();
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
//context.fillRect(5, 5, canvas.width-10, canvas.height-10);
|
||||
};
|
||||
|
||||
drawGraphics();
|
||||
|
||||
// Rotate the graphics
|
||||
function rotateGraphics(percent) {
|
||||
var radians = (360*percent)/180*Math.PI;
|
||||
var x = Math.round(canvas.width*.5 + Math.cos(radians) * (canvas.width*.45) - canvas.height*.5);
|
||||
var y = Math.round(canvas.height*.5 + Math.sin(radians) * (canvas.height*.45) - canvas.height*.5);
|
||||
context.save();
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
context.translate(canvas.width*0.5, canvas.height*0.5);
|
||||
context.rotate(radians);
|
||||
context.translate(-canvas.width*0.5, -canvas.height*0.5);
|
||||
drawGraphics();
|
||||
context.restore();
|
||||
};
|
||||
|
||||
// Handle the mouse move event
|
||||
canvas.onmousemove = function(event){
|
||||
var offsetX = 8;
|
||||
var percent = Math.floor((event.x - offsetX) / canvas.width * 100) / 100;
|
||||
rotateGraphics(percent);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user