Coder Social home page Coder Social logo

gury's Introduction

gury (v 0.1) - A jQuery inspired canvas utility library

by Ryan Sandor Richards

Gury (pronounced "jury") is a simple to use utility library for drawing, animating, and managing HTML5 canvas tags. The goal is to support the HTML5 Canvas API with a framework that allows for faster/easier application development.

It was built with simplicity in mind and its usage was modeled in the image of jQuery. For instance you can initialize, style, and animate an entire scene in a single expression using chaining.

Example

Here is an example of some gury code to make a simple spinning square animation.

$g('screen').size(100, 100).add({
    theta: 0, 
    draw: function(ctx) {
        ctx.save();
        ctx.translate(50, 50);
        ctx.rotate(this.theta);
        ctx.fillStyle = "#ada";
        ctx.fillRect(-32, -32, 64, 64);
        ctx.restore();
        this.theta += Math.PI / 120;
    }
}).play(16);

Neat, eh?

Documentation

You can find full API documentation for the project on it's github wiki: http://github.com/rsandor/gury/wiki/API

License (MIT)

Copyright (c) 2010 Ryan Sandor Richards

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

gury's People

Contributors

brucespang avatar rsandor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

gury's Issues

.mouseenter/.mouseleave not working?

Hello there, I'm really enjoying this library, but I .mouseenter/.mouseleave isn't working for some reason. Any ideas?

Code:

<!DOCTYPE html><html>
  <head>
    <title>Guryyyyy</title>
    <style>
        * {
            margin:0;
            padding:0;
        }
        html, body{
                width: 100%;
                height: 100%;
                overflow: hidden;
            }   
    </style>
    <script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="http://github.com/downloads/rsandor/gury/gury.min.js"></script>
  </head>
  <body>

  </body>

<script type="text/javascript" charset="utf-8">
function coordinates(x, y) {
    if (typeof x === "undefined") {
        this.x = 0;
    } else {
        this.x = x;
    }

    if (typeof y === "undefined") {
        this.y = 0;
    } else {
        this.y = y;
    }
};

var ANCHOR_TOP_LEFT = new coordinates();
var ANCHOR_TOP_RIGHT = new coordinates(window.innerWidth);
var ANCHOR_BOT_LEFT = new coordinates(0, window.innerHeight);
var ANCHOR_BOT_RIGHT = new coordinates(window.innerWidth, window.innerHeight);

g = $g().size(ANCHOR_BOT_RIGHT.x, ANCHOR_BOT_RIGHT.y).background("black").place('body');

function boxy(width) {
    this.width = width;
}

boxy.prototype.draw = function(ctx,canvas) {
    ctx.fillStyle = "#FFF";
    ctx.beginPath();
    ctx.rect((0.05*ANCHOR_BOT_RIGHT.x),(0.05*ANCHOR_BOT_RIGHT.y),this.width,(0.9*ANCHOR_BOT_RIGHT.y));
    ctx.fill();
};

g.add('box', new boxy(100));
//Click works great
//g.click('box', function(e){this.width += 10;});
g.mouseenter('box', function(e){this.width += 10;});
g.play(32);
</script>
</html>

Code error in the API intro

Your second example I think should start with :

$g("canvasOne")

It is currently missing the g.

I am having fun learning the library. Good stuff.

.each should have a callback with index and object, not object and index

.each currently uses a callback where arguments are in order object, index, but jQuery for its .each uses first index and then object. It is really unintuitive to use different arguments order in the code for a similar method, so I would propose an API change, .each should use a callback with arguments in order same as in jQuery: first index, then object.

Automatically delegating events to added objects?

I'm adding objects to my canvas, and they have their own update and draw functions, which get called automatically after play() is started. However, I also want them have their own functions for event handling, such as mouseenter, mousedown, etc.

So instead of

$g().add('myGuy', new Guy());
$g().mousedown('myGuy', handleMouseDown());

The mousedown events will be delegated to the objects themselves, so adding myGuy to the canvas will let it start receiving events automatically, like this:

$g().add('myGuy', new Guy());
// now when the mouse enters the Guy object's space, it will call the Guy object's mouseEnter function, etc.

I don't see anyway to set this up currently, is it possible, or can it be made possible?

Unify play and draw

Currently it is really cumbersome to switch between play and draw. Most of the time I have a static drawing, but for some effects I use animation. Because I would like to keep CPU usage for clients low I do not want to "play" all the time.

So I have a proposal: let objects added to the drawing queue have additional method "animate" which returns true or false if it is necessary to animate it (and call "update" and so on). Then provide additional method (to keep things backwards compatible) called "run", which would do something like every few ms: go trough all objects and call "animate". If any object returns true, call update on it. At the end, if any object returned true from "animate", redraw. Otherwise do nothing.

IE9 __defineGetter__ issues

When running in IE9 the script falls over due to IE9 and EC5 not supporting defineGetter method.

This is a simple fix:

this.__defineGetter__("length", function() { return length; });

becomes:

Object.defineProperty(this, "length", {
    get: function() {
        return length;
    }
});

And then:

this.__defineGetter__("length", function() { return table.length; });

becomes:

Object.defineProperty(this, "length", {
    get: function() {
        return table.length;
    }
});

Thanks for a nice library by the way!

Same Game

Could I get the demo code for same game?

Bezier curves?

Hi,

Is it possible to draw and animate bezier curves with this library?

Thx,

Clement

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.