Graphical Plotter //JavaScript Repository

Description

Draws lines and arcs using javascript.
Created: 2005.08.08

Code (Download)

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/dhtml/graphical-plotter [rev. #1]

Canvas = function(){
    var o = this;
    (o.penPos = {x: 0, y: 0}, o.pixelSize = 10, o.pen = {style: "solid",
        size: 1, color: "#000"}, o.brush = {style: "solid", color: "#000"});
};
with({p: Canvas.prototype}){
    p.pixel = function(x, y, color) {
        var o = this, s = document.body.appendChild(document.createElement("div")).style;
        return (s.position = "absolute", s.width = (o.pen.size * o.pixelSize) + "px",
            s.height = (o.pen.size * o.pixelSize) + "px", s.fontSize = "1px",
            s.left = (x * o.pixelSize) + "px", s.top = (y * o.pixelSize) + "px",
            s.backgroundColor = color || o.pen.color, o);
    };
    p.line = function(x1, y1, x2, y2){
        if(Math.abs(x1 - x2) < Math.abs(y1 - y2))
            for(y = Math.min(y1, y2) - 1, x = Math.max(y1, y2); ++y <= x;
                this.pixel((y * (x1 - x2) - x1 * y2 + y1 * x2) / (y1 - y2), y));
        else
            for(x = Math.min(x1, x2) - 1, y = Math.max(x1, x2); ++x <= y;
                this.pixel(x, (x * (y1 - y2) - y1 * x2 + x1 * y2) / (x1 - x2)));
        return this;
    };
    p.arc = function(x, y, raio, startAngle, degrees) {
        for(degrees += startAngle; degrees --> startAngle;
            this.pixel(Math.cos(degrees * Math.PI / 180) * raio + x,
                Math.sin(degrees * Math.PI / 180) * raio + y)); return this;
    };
    p.rectangle = function(x, y, width, height, rotation){
        return this.moveTo(x, y).lineBy(0, height).lineBy(width, 0).lineBy(0, -height).lineBy(-width, 0);
    };
    p.moveTo = function(x, y){var o = this; return (o.penPos.x = x, o.penPos.y = y, o);};
    p.moveBy = function(x, y){var o = this; return o.moveTo(o.penPos.x + x, o.penPos.y + y);};
    p.lineTo = function(x, y){var o = this; return o.line(o.penPos.x, o.penPos.y, x, y).moveTo(x, y);};
    p.lineBy = function(x, y){var o = this; return o.lineTo(o.penPos.x + x, o.penPos.y + y);};
    p.curveTo = function(cx, cy, x, y){};
    p.polyBezier = function(points){};
    p.path = function(points){};
}

Example (Example)

<script type="text/javascript">
//<![CDATA[

canvas = new Canvas;

canvas.pen.color = "#f00";
canvas.rectangle(30, 20, 20, 20);
canvas.pen.color = "#080";
canvas.rectangle(35, 25, 10, 10);
canvas.pen.color = "#008";
canvas.arc(50, 30, 10, 180, 270);
canvas.arc(30, 30, 10, 0, 270);
canvas.pen.color = "#ff0";

//]]>
</script>

Help

Constructor

Canvas(void)
Generates an instance of Canvas.

Properties

Canvas.penPos.x: Integer
Current "x" coord of the pen.
Canvas.penPos.y: Integer
Current "y" coord of the pen.
Canvas.pen.style: String
Pen style (it should be setted with css border styles: "dashed", "solid", ...).
Canvas.pen.size: Integer
Pen width.
Canvas.pen.color: String
Pen color.
Canvas.pixelSize: Integer
Imaginary size of the pixel in the screen.
Canvas.brush.*
Not supported.

Methods

Canvas.pixel(x: Integer, y: Integer): Canvas
Draws a pixel in the position (x,y) and returns the own canvas instance.
Canvas.moveTo(x: Integer, y: Integer): Canvas
Moves the pen to the position (x,y) and returns the own canvas instance.
Canvas.moveBy(x: Integer, y: Integer): Canvas
Moves the pen to the position (penPos.x + x, penPos.y + y ) and returns the own canvas instance.
Canvas.lineTo(x: Integer, y: Integer): Canvas
Draws a line from (penPos.x, penPos.y) to (x,y) and returns the own canvas instance.
Canvas.lineBy(x: Integer, y: Integer): Canvas
Draws a line from (penPos.x, penPos.y) to (penPos.x + x, penPos.y + y ) and returns the own canvas instance.
Canvas.line(x1: Integer, y1: Integer, x2: Integer, y2: Integer): Canvas
Draws a line from (x1,y1) to (x2,y2) and returns the own canvas instance.
Canvas.arc(x: Integer, y: Integer, ray: Integer, startAngle: Integer, degrees: Integer): Canvas
Draws an arc starting from the angle "startAngle" to "startAngle + degrees", with center in (x,y), having ray equals to "ray" and returns the own canvas instance.
Canvas.rectangle(x: Integer, y: Integer, width: Integer, height: Integer, rotation: Integer): Canvas
Draws a square with origin in (x,y), width equals to "width", height equals to "height" (rotation não é suportado) and returns the own canvas instance.
Canvas.curveTo(cx: Integer, cy: Integer, x: Integer, y: Integer): Canvas
Not supported.
Canvas.polyBezier(points: Array): Canvas
Not supported.
Canvas.path(points: Array): Canvas
Not supported.

Rank (Votes: 45)

3.27