Post Reply 
EZDraw a simple 2D graphical programming environment
12-15-2023, 01:09 AM
Post: #1
EZDraw a simple 2D graphical programming environment
EZDraw is a simple 2D graphical programming environment, based on the HTML5 canvas. It allows you to make simple drawings and animations with very little code.





Reference commands
https://davidprograma.github.io/ezdraw/v1/reference/

sample: Multi-body orbit:

online execution

PHP Code:
function init(system) {
    
let lim 1.5;
    
xlimits(-limlim);    // horiz limits for drawing
    
ylimits(-limlim);    //  vert limits for drawing
    
    
let sun = new Body();  // 'Sun'
    
sun.color "#FE4";    // color
    
sun.0.05;          // radius
    // using defaults for other sun parameters
    
    
let mars = new Body(); // 'Mars' body
    
mars.px 1;           // initial position, x component
    
mars.vy 0.85;         // initial position, y component
    
mars.0.02;         // radius
    
mars.0.000001;     // mass
    
mars.color "#951";   // color
    
    
system.add(sun);               // add Sun to system
    
system.add(mars);              // add Mars to system
}

class 
Body {
    
constructor() {
        
this.px this.py 0// position
        
this.vx this.vy 0// velocity
        
this.ax this.ay 0// acceleration
        
this.1;            // mass
        
this.0.1;          // radius
        
this.color 'white';  // color
    
}
    
update(dt) {
        
this.vx += this.ax dt;  // acceleration affects...
        
this.vy += this.ay dt;  // ...velocity;
        
this.px += this.vx dt;  // velocity affects...
        
this.py += this.vy dt;  // ...position
    
}
    
draw() {
        
linecolor(null);                  // no line        
        
fillcolor(this.color);            // fill color
        
circle(this.pxthis.pythis.r); // circle
    
}
}

class 
BodySystem {
    
constructor() {
        
this.bodies = [];  // array of bodies
        
this.1;        // gravitation constant
    
}
    
add(body) {
        
this.bodies.push(body); // add body to array
    
}
    
update(dt) {
        
// set all accelerations to zero
        
for (let i 0this.bodies.lengthi++)
            
this.bodies[i].ax this.bodies[i].ay 0;
        
// calculate accelerations for pairs of bodies
        
for (let i 0this.bodies.lengthi++)
            for (
let j i+1this.bodies.lengthj++)
                
this.calcAcc(this.bodies[i], this.bodies[j]);
        
// update velocities and positions for all bodies
        
for (let i 0this.bodies.lengthi++)
            
this.bodies[i].update(dt);
    }
    
calcAcc(body1body2) {
        
let dx body2.px body1.px// distance x component
        
let dy body2.py body1.py// distance y component
        
let d2 dx dx dy dy;   // squared distance
        
let d Math.sqrt(d2);        // distance
        
let ux dx d;              // unitary vector, x comp
        
let uy dy d;              // unitary vector
        
let a1 this.body2.d2// scalar acc, body 1
        
let a2 this.body1.d2// scalar acc, body 2
        
body1.ax += ux a1;  // body 1 acc, x component
        
body1.ay += uy a1;  // body 1 acc, y component
        
body2.ax += -ux a2// body 2 acc, x component
        
body2.ay += -uy a2// body 2 acc, y component
    
}
    
draw() {
        for (
let i 0this.bodies.lengthi++)
            
this.bodies[i].draw();
    }
}

let system = new BodySystem(); // Body System
init(system);

function 
draw() {               // draw function for ezdraw
    
clear('black');             // paint it black
    
system.draw();              // draw body system
    
system.update(deltatime()); // update positions in body system
    
redraw();                   // request redraw

Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)