Skip to content

Commit

Permalink
Added instance persistence layer, updated demos, and added clear meth…
Browse files Browse the repository at this point in the history
…od with documentation.
  • Loading branch information
rsandor committed Oct 12, 2010
1 parent 2961dd5 commit 97d9f80
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
scratch_pad
12 changes: 12 additions & 0 deletions README
Expand Up @@ -52,6 +52,18 @@ Creates a new canvas node and returns the gury object representing that canvas.
Finds a canvas on the page with the given id and returns a gury object
representing that canvas.

### .canvas

The canvas node represented by the gury object. Example:

$g().canvas.style.border = "5px solid red";

### .ctx

The graphics context for the canvas represented by the gury object. Example:

$g().ctx.fillStyle = "#a0a";

### .place(node)

This method is used to place canvas objects created using gury. If the `node`
Expand Down
16 changes: 16 additions & 0 deletions README.markdown
Expand Up @@ -52,6 +52,18 @@ Creates a new canvas node and returns the gury object representing that canvas.
Finds a canvas on the page with the given id and returns a gury object
representing that canvas.

### .canvas

The canvas node represented by the gury object. Example:

$g().canvas.style.border = "5px solid red";

### .ctx

The graphics context for the canvas represented by the gury object. Example:

$g().ctx.fillStyle = "#a0a";

### .place(node)

This method is used to place canvas objects created using gury. If the `node`
Expand Down Expand Up @@ -100,6 +112,10 @@ upon which to draw and optionally a reference to the canvas node:

Renders the scene by drawing all of the added objects in order on the canvas.

### .clear()

Clears the canvas (does *not* remove any objects bound to the Gury instance).

### .play(interval)

Renders the scene repeatedly at fixed intervals creating an animation. This
Expand Down
2 changes: 1 addition & 1 deletion demos/demo.html
Expand Up @@ -23,7 +23,7 @@ <h1>gury demo</h1>
<script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<!-- Include the gury library -->
<script type="text/javascript" charset="utf-8" src="gury.js"></script>
<script type="text/javascript" charset="utf-8" src="../gury.js"></script>

<!-- Now script up a neato canvas scene! -->
<script type="text/javascript" charset="utf-8">
Expand Down
2 changes: 1 addition & 1 deletion demos/demo2.html
Expand Up @@ -23,7 +23,7 @@ <h1>gury demo 2</h1>
<script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<!-- Include the gury library -->
<script type="text/javascript" charset="utf-8" src="gury.js"></script>
<script type="text/javascript" charset="utf-8" src="../gury.js"></script>

<!-- Now script up a neato canvas scene! -->
<script type="text/javascript" charset="utf-8">
Expand Down
2 changes: 1 addition & 1 deletion demos/demo3.html
Expand Up @@ -26,7 +26,7 @@ <h1>gury demo 3</h1>
<script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<!-- Include the gury library -->
<script type="text/javascript" charset="utf-8" src="gury.js"></script>
<script type="text/javascript" charset="utf-8" src="../gury.js"></script>

<!-- Now script up a neato canvas scene! -->
<script type="text/javascript" charset="utf-8">
Expand Down
4 changes: 3 additions & 1 deletion demos/demo4.html
Expand Up @@ -26,7 +26,7 @@ <h1>gury game</h1>
<script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<!-- Include the gury library -->
<script type="text/javascript" charset="utf-8" src="gury.js"></script>
<script type="text/javascript" charset="utf-8" src="../gury.js"></script>

<!-- Now script up a neato canvas scene! -->
<script type="text/javascript" charset="utf-8">
Expand Down Expand Up @@ -91,6 +91,8 @@ <h1>gury game</h1>
gury.draw();
});

gury.canvas.id = "gameboard";

reset();

$('#reset').click(reset);
Expand Down
94 changes: 90 additions & 4 deletions gury.js
Expand Up @@ -22,6 +22,45 @@
THE SOFTWARE.
*/
window.$g = (function() {
/*
* Utility functions
*/
function isObject(v) { return typeof v == "object"; }
function isFunc(v) { return typeof v == "function"; }
function isString(v) { return typeof v == "string"; }
function isObjectOrFunc(v) { return typeof v == "function" || typeof v == "object"; }

/*
* These handle mappings from Canvas DOM elements to Gury instances
* to allow for persistant states between calls to the module.
*/
var guryId = 1;
var canvasToGury = {};

function nextGuryId() {
return "gury_id_" + (guryId++);
}

function getGury(canvas) {
if (!isString(canvas._gury_id) || !(canvasToGury[canvas._gury_id] instanceof Gury)) {
return null;
}
return canvasToGury[canvas._gury_id];
}

function setGury(canvas, gury) {
var gid;

if (typeof canvas._gury_id == "string") {
gid = canvas._gury_id;
}
else {
gid = canvas._gury_id = nextGuryId();
}

return canvasToGury[gid] = gury;
}

/*
* Core Gury Class
*/
Expand All @@ -30,11 +69,22 @@ window.$g = (function() {
if (canvas == null) {
canvas = document.createElement('canvas');
}

// Check for an existing mapping from the canvas to a Gury instance
if (getGury(canvas)) {
return getGury(canvas);
}

// Otherwise create a new instance
this.canvas = canvas;
this.ctx = canvas.getContext('2d');

this._objects = [];
this._groups = {};
this._paused = false;
this._loop_interval = null;

return setGury(canvas, this);
}

Gury.prototype.place = function(node) {
Expand Down Expand Up @@ -69,13 +119,49 @@ window.$g = (function() {
* Objects and Rendering
*/

Gury.prototype.add = function(d) {
this._objects.push(d);
function _add(gury, name, object) {
if (name) {
var parts = name.split('.');
for (var i=0; i < parts.length; i++) {

}
}

gury._objects.push(object);
}

Gury.prototype.add = function() {
var name = null, obj;

if (arguments.length < 1) {
return this;
}
else if (arguments.length < 2) {
obj = arguments[0];
if (!isObjectOrFunc(obj)) {
return this;
}
}
else {
name = arguments[0];
obj = arguments[1];
if (!isString(name) || !isObjectOrFunction(obj)) {
return this;
}
}

_add(this, name, obj);

return this;
};

Gury.prorotype.clear = function() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
return this;
};

Gury.prototype.draw = function() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.clear();
for (var i = 0; i < this._objects.length; i++) {
var ob = this._objects[i];
if (typeof ob == "function") {
Expand All @@ -95,7 +181,7 @@ window.$g = (function() {
Gury.prototype.play = function(interval) {
// Ignore multiple play attempts
if (this._loop_interval != null) {
return;
return this;
}

var _gury = this;
Expand Down

0 comments on commit 97d9f80

Please sign in to comment.