-
Notifications
You must be signed in to change notification settings - Fork 0
2. Instances
You can add an Instance
class to your code by this:
import { Canvas, Instance } from '../je/index.js';
or:
import { Instance } from '../je/instance.js';
Instance
is a game object.
class MyObject extends Instance {
onDraw() {
this.fillColor = 'red';
this.rectangle(0, 0, 20, 20); // rectangle(x, y, width, height). x and y are 0.
this.fill();
}
}
Now you can add it to game:
new MyObject().addToMain();
Now you can see a small red rectangle in the left top corner.
Full code:
import { Canvas, Instance } from '../je/index.js';
const cv = new Canvas({
width: 1366,
height: 768
});
class MyObject extends Instance {
onDraw() {
this.fillColor = 'red';
this.rectangle(0, 0, 20, 20);
this.fill();
}
}
new MyObject().addToMain();
cv.start();
You can add a Scene
class to your code by this:
import { Canvas, Scene } from '../je/index.js';
Scene is an game object too. But it automatically adds to canvas and can has child elements.
Here's an example:
import { Canvas, Instance, Scene } from '../je/index.js';
const cv = new Canvas({
width: 1366,
height: 768
});
class MyObject extends Instance {
onDraw() {
this.fillColor = 'red';
this.rectangle(0, 0, 20, 20);
this.fill();
}
}
class MyGame extends Scene {
onCreate() { // this event is called only once at start!
this.add(new MyObject());
}
}
new MyGame().addToMain();
cv.start();
You can see a small red rectangle too.
Warning
Don't use scenes for game objects, use only one scene to render all your objects (e.g. main menu scene, game scene, settings scene, etc.).
this.rectangle(x, y, width, height); // draw a rectangle at an angle to the center.
this.roundRect(x, y, width, height, radius); // draw a rounded rectangle at an angle to the center.
this.circle(x, y, radius); // draw a circle at an angle to the center.
Important
At the moment, the polygon
method not used at an angle to the center.
this.polygon(N); // draw a polygon with N-th number of edges.
this.polygon(N, { scale: 2 });
this.polygon(N, { rotation: 45 });
this.polygon(N, { scale: 2, rotation: 30 });
Important
At the moment, the path
method not used at an angle to the center.
this.path([new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(0, 1)]); // draw a rectangle.
Draw an image (required to create it).
this.drawImage(image, x, y, width, height);
this.fillText(x, y, ...text);
this.fillText(x, y, 'Number:', 1); // fill "Number: 1" text in x, y position
this.strokeText(x, y, ...text);
this.strokeText(x, y, 'Number:', 1); // stroke "Number: 1" text in x, y position
const metrics = this.measureText(...text);
this.translate(x, y);
this.rotate(radians); // rotate in radians
this.rotateAngle(angle); // rotate in angle (0 - 360)
this.scale(xscale, yscale);
this.save();
{
this.translate(x, y); // you can change it on rotate or scale too.
/* code translated with x, y */
}
this.restore();
/* code translated as default */
Fill in the selected area.
this.fill();
this.fill('red'); // fast fillColor
Stroke in the selected area.
this.stroke();
this.stroke('blue'); // fast strokeColor
Delete the instance.
this.destroy();
Add an instance to this instance.
this.add(instance);
Add an instance to the canvas.
this.addToMain();
Return the Rect
class.
get rect() { // `get rect` is used in collisions.
return this.getRect(width, height); // now your object has collisions.
}
Is one of selected classes parent of this class
if (this.isClassOf(MyObject)) { // example
console.log('my parent is the MyObject class');
}
Change the width of the lines for the stroke
method.
this.lineWidth = 10;
Set align for text.
this.fontAlign = 'center';
The same as fontAlign
but horizontally.
this.fontBaseline = 'middle';
this.font = '32px Intel'; // '<font resolution>px <font name>'
this.fillColor = 'red';
this.fillColor = [255, 0, 0]; // RGB
this.fillColor = Color.create(255, 0, 0); // don't forget to import `Color`
The same as fillColor
but for the stroke
method.
this.strokeColor = 'red';
this.alpha = 0.1;
/* code */
this.alpha = 1; // reset alpha
Get the original context of canvas.
const ctx = this.ctx;
// or
const { ctx } = this;
...
ctx.rotate(45 / 180 * Math.PI); // rotate it at a 45-degree angle.
Get absolute position of instance.
const pos = this.pos;
// or
const { pos } = this;
this.pos = new Point(10, 10); // don't forget to import `Point`.
const canvas = this.canvas;
// or
const { canvas } = this;
this.drawDirection = 'top-left'; // center is default
Unique ID for every instance.
"Layer" of instance
onCreate() {
this.depth = 1;
}
X position
Y position
Clips the stroke if enabled.
onDraw() {
this.circle(0, 0, 64, 64);
this.fill('white');
this.clipStroke = true;
this.lineWidth = 4;
this.stroke('rgba(0, 0, 0, 0.2)');
this.clipStroke = false;
}
onCreate() {
this.dontTranslate = true; // disables x, y. Default is false
}
onCreate() {
this.drawChildBottom = false; // enable drawing of children on top instead of bottom. Default is true
}
Important
This wiki page has been updated for Jurtan Engine 1.4-beta