A nifty Scenegraph library for canvas 2d html rendering.
|
6 lat temu | |
---|---|---|
build | 6 lat temu | |
docs | 6 lat temu | |
examples | 6 lat temu | |
readme | 6 lat temu | |
source | 6 lat temu | |
.gitignore | 6 lat temu | |
LICENSE | 6 lat temu | |
README.md | 6 lat temu | |
_config.yml | 6 lat temu | |
package.json | 6 lat temu | |
rollup.config.js | 6 lat temu |
The code bellow shows how a canvas could be configured for a full screen setup.
var canvas = document.createElement("canvas");
canvas.style.width = "100%";
canvas.style.height = "100%";
canvas.style.top = "0px";
canvas.style.left = "0px";
canvas.style.position = "absolute";
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
document.body.onresize = function()
{
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
The coordinate system used, is the same as if using the canvas API directly -Y is down relative to the origin and +X is left relative to the origin. All objects are positioned using this coordinate system.
ignoreViewport
flag to false, this will indicate that renderer to reset the viewport transform just for that object.draw(context, viewport, canvas)
and its transform(context, viewport, canvas)
methods.draw(context, viewport, canvas)
function is where the object gets draw into the screen, here you can implement your custom object as if it was drawn alone in a canvas.transform(context, viewport, canvas)
is where the object matrix gets applied to the canvas drawing context, it is assumed that the viewport transformation was pre-applied.Example of a custom element, drawing a custom box with a red gradient box. Its also possible to extend other base objects like Box
that already includes the necessary code for mouse events.
var object = new Trenette.Object2D();
object.draw = function(context, viewport, canvas)
{
// Create gradient
var grd = context.createLinearGradient(0, 0, 70, 0);
grd.addColorStop(0, "#FF0000");
grd.addColorStop(1, "#FFFFFF");
// Fill with gradient
context.fillStyle = grd;
context.fillRect(-70, 70, 140, 140);
};
The system supports multiple pointer events that can be used to control the objects and interact with the users.
// Called when the pointer enters the object.
onPointerEnter(pointer, viewport);
// Called when the was inside of the object and leaves the object.
onPointerLeave(pointer, viewport);
// Called while the pointer is over (inside) of the object.
onPointerOver(pointer, viewport);
// Called when the object is dragged across the screen, only works if the object has the property draggable set true.
// Delta is the movement of the pointer already translated into local object coordinates.
onPointerDrag(pointer, viewport, delta);
// Called while the pointer button is pressed.
onButtonPressed(pointer, viewport);
// Called when the pointer button is pressed down.
onButtonDown(pointer, viewport);
// Called after the pointer button gets released.
onButtonUp(pointer, viewport);
DOM objects contain a div element
inside that can be used to attach custom code, by default that element pointerEvents
CSS style is set to none, disabling all the pointer events, it can be set to auto to re-enable them.
var dom = new Trenette.DOM(division);
dom.size.set(100, 50);
dom.origin.set(50, 25);
group.add(dom);
// Re-enable DOM pointer events
dom.element.style.pointerEvents = "auto";
// Attach a new DOM element to the DOM object
var text = document.createElement("div");
text.style.fontFamily = "Arial";
text.style.textAlign = "center";
text.innerHTML = "DOM text!";
dom.element.appendChild(text);