123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>JSDoc: Source: Renderer.js</title>
- <script src="scripts/prettify/prettify.js"> </script>
- <script src="scripts/prettify/lang-css.js"> </script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
- <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
- </head>
- <body>
- <div id="main">
- <h1 class="page-title">Source: Renderer.js</h1>
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>import {Pointer} from "./input/Pointer.js";
- import {ViewportControls} from "./controls/ViewportControls.js";
- import {AnimationTimer} from "./utils/AnimationTimer";
- import {EventManager} from "./utils/EventManager";
- /**
- * The renderer is responsible for drawing the objects structure into the canvas element and manage its rendering state.
- *
- * Object are updated by the renderer before drawing, the renderer sorts the objects by layer, checks for pointer events and draw the objects into the screen.
- *
- * Input handling is also performed by the renderer (it is also used for the event handling).
- *
- * @class
- * @param {Element} canvas Canvas to render the content to.
- * @param {Object} options Renderer canvas options.
- */
- function Renderer(canvas, options)
- {
- if(options === undefined)
- {
- options =
- {
- alpha: true,
- disableContextMenu: true,
- imageSmoothingEnabled: true,
- imageSmoothingQuality: "low",
- globalCompositeOperation: "source-over"
- };
- }
- /**
- * Event manager for DOM events created by the renderer.
- *
- * Created automatically when the renderer is created. Disposed automatically when the renderer is destroyed.
- *
- * @type {EventManager}
- */
- this.manager = new EventManager();
- if(options.disableContextMenu) {
- this.manager.add(canvas, "contextmenu", function(e) {
- e.preventDefault();
- e.stopPropagation();
- });
- }
- this.manager.create();
- /**
- * Canvas DOM element, the user needs to manage the canvas state.
- *
- * The canvas size (width and height) should always match its actual display size (adjusted for the device pixel ratio).
- *
- * @type {Element}
- */
- this.canvas = canvas;
- /**
- * Division where DOM and SVG objects should be placed at. This division should be perfectly aligned whit the canvas element.
- *
- * If no division is defined the canvas parent element is used by default to place these objects.
- *
- * The DOM container to be used can be obtained using the getDomContainer() method.
- *
- * @type {Element}
- */
- this.container = null;
- /**
- * Canvas 2D rendering context used to draw content.
- *
- * The options passed thought the constructor are applied to the context created.
- *
- * @type {CanvasRenderingContext2D}
- */
- this.context = this.canvas.getContext("2d", {alpha: options.alpha});
- this.context.imageSmoothingEnabled = options.imageSmoothingEnabled;
- this.context.imageSmoothingQuality = options.imageSmoothingQuality;
- this.context.globalCompositeOperation = options.globalCompositeOperation;
- /**
- * Pointer input handler object, automatically updated by the renderer.
- *
- * The pointer is attached to the DOM window and to the canvas provided by the user.
- *
- * @type {Pointer}
- */
- this.pointer = new Pointer(window, this.canvas);
- /**
- * Indicates if the canvas should be automatically cleared before new frame is drawn.
- *
- * If set to false the user should clear the frame before drawing.
- *
- * @type {boolean}
- */
- this.autoClear = true;
- }
- /**
- * Get the DOM container to be used to store DOM and SVG objects.
- *
- * Can be set using the container attribute, by default the canvas parent element is used.
- *
- * @returns {Element} DOM element selected for objects.
- */
- Renderer.prototype.getDomContainer = function()
- {
- return this.container !== null ? this.container : this.canvas.parentElement;
- };
- /**
- * Creates a infinite render loop to render the group into a viewport each frame.
- *
- * Automatically creates a viewport controls object, used for the user to control the viewport.
- *
- * The render loop can be accessed trough the animation timer returned. Should be stopped when no longer necessary to prevent memory/code leaks.
- *
- * @param {Object2D} group Object to be rendered, alongside with all its children. Object2D can be used as a container to group objects.
- * @param {Viewport} viewport Viewport into the scene.
- * @param {Function} onUpdate Function called before rendering the frame, can be used for additional logic code. Object logic should be directly written in the update method of objects.
- * @return {AnimationTimer} Animation timer created for this render loop. Should be stopped when no longer necessary.
- */
- Renderer.prototype.createRenderLoop = function(group, viewport, onUpdate)
- {
- var self = this;
- var controls = new ViewportControls(viewport);
- var timer = new AnimationTimer(function()
- {
- if(onUpdate !== undefined)
- {
- onUpdate();
- }
- controls.update(self.pointer);
- self.update(group, viewport);
- });
- timer.start();
- return timer;
- };
- /**
- * Dispose the renderer object, clears the pointer events attached to the window/canvas.
- *
- * Should be called if the renderer is no longer in use to prevent code/memory leaks.
- */
- Renderer.prototype.dispose = function(group, viewport, onUpdate)
- {
- this.manager.destroy();
- this.pointer.dispose();
- };
- /**
- * Renders a object using a user defined viewport into a canvas element.
- *
- * Before rendering automatically updates the input handlers and calculates the objects/viewport transformation matrices.
- *
- * The canvas state is saved and restored for each individual object, ensuring that the code of one object does not affect another one.
- *
- * Should be called at a fixed rate preferably using the requestAnimationFrame() method, its also possible to use the createRenderLoop() method, that automatically creates a infinite render loop.
- *
- * @param object {Object2D} Object to be updated and drawn into the canvas, the Object2D should be used as a group to store all the other objects to be updated and drawn.
- * @param viewport {Viewport} Viewport to be updated (should be the one where the objects will be rendered after).
- */
- Renderer.prototype.update = function(object, viewport)
- {
- // Get objects to be rendered
- var objects = [];
- // Traverse object and get all objects into a list.
- object.traverse(function(child)
- {
- if(child.visible)
- {
- objects.push(child);
- }
- });
- // Sort objects by layer
- objects.sort(function(a, b)
- {
- if(b.layer === a.layer)
- {
- return b.level - a.level;
- }
-
- return b.layer - a.layer;
- });
- // Pointer object update
- var pointer = this.pointer;
- pointer.update();
- // Viewport transform matrix
- viewport.updateMatrix();
- // Project pointer coordinates
- var point = pointer.position.clone();
- var viewportPoint = viewport.inverseMatrix.transformPoint(point);
- // Object pointer events
- for(var i = 0; i < objects.length; i++)
- {
- var child = objects[i];
-
- //Process the object pointer events
- if(child.pointerEvents)
- {
- // Calculate the pointer position in the object coordinates
- var localPoint = child.inverseGlobalMatrix.transformPoint(child.ignoreViewport ? point : viewportPoint);
- // Check if the pointer pointer is inside
- if(child.isInside(localPoint))
- {
- // Pointer enter
- if(!child.pointerInside && child.onPointerEnter !== null)
- {
- child.onPointerEnter(pointer, viewport);
- }
- // Pointer over
- if(child.onPointerOver !== null)
- {
- child.onPointerOver(pointer, viewport);
- }
- // Double click
- if(pointer.buttonDoubleClicked(Pointer.LEFT) && child.onDoubleClick !== null)
- {
- child.onDoubleClick(pointer, viewport);
- }
- // Pointer pressed
- if(pointer.buttonPressed(Pointer.LEFT) && child.onButtonPressed !== null)
- {
- child.onButtonPressed(pointer, viewport);
- }
- // Just released
- if(pointer.buttonJustReleased(Pointer.LEFT) && child.onButtonUp !== null)
- {
- child.onButtonUp(pointer, viewport);
- }
- // Pointer just pressed
- if(pointer.buttonJustPressed(Pointer.LEFT))
- {
- if(child.onButtonDown !== null)
- {
- child.onButtonDown(pointer, viewport);
- }
- // Drag object and break to only start a drag operation on the top element.
- if(child.draggable)
- {
- child.beingDragged = true;
- if(child.onPointerDragStart !== null)
- {
- child.onPointerDragStart(pointer, viewport);
- }
- break;
- }
- }
- child.pointerInside = true;
- }
- else if(child.pointerInside)
- {
- // Pointer leave
- if(child.onPointerLeave !== null)
- {
- child.onPointerLeave(pointer, viewport);
- }
- child.pointerInside = false;
- }
- // Stop object drag
- if(pointer.buttonJustReleased(Pointer.LEFT))
- {
- if(child.draggable)
- {
- // On drag end callback
- if(child.beingDragged === true && child.onPointerDragEnd !== null)
- {
- child.onPointerDragEnd(pointer, viewport);
- }
- child.beingDragged = false;
- }
- }
- }
- }
- // Object drag events and update logic
- for(var i = 0; i < objects.length; i++)
- {
- var child = objects[i];
- // Pointer drag event
- if(child.beingDragged)
- {
- if(child.onPointerDrag !== null)
- {
- var lastPosition = pointer.position.clone();
- lastPosition.sub(pointer.delta);
- // Get position and last position in world space to calculate world pointer movement
- var positionWorld = viewport.inverseMatrix.transformPoint(pointer.position);
- var lastWorld = viewport.inverseMatrix.transformPoint(lastPosition);
- // Pointer movement delta in world coordinates
- var delta = positionWorld.clone();
- delta.sub(lastWorld);
- child.onPointerDrag(pointer, viewport, delta, positionWorld);
- }
- }
- // On update
- if(child.onUpdate !== null)
- {
- child.onUpdate();
- }
- }
- // Update transformation matrices
- object.traverse(function(child)
- {
- child.updateMatrix();
- });
- this.context.setTransform(1, 0, 0, 1, 0, 0);
-
- // Clear canvas content
- if(this.autoClear)
- {
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
- }
- // Render into the canvas
- for(var i = objects.length - 1; i >= 0; i--)
- {
- if(objects[i].isMask)
- {
- continue;
- }
- if(objects[i].saveContextState)
- {
- this.context.save();
- }
- // Apply all masks
- var masks = objects[i].masks;
- for(var j = 0; j < masks.length; j++)
- {
- if(!masks[j].ignoreViewport)
- {
- viewport.matrix.setContextTransform(this.context);
- }
- masks[j].transform(this.context, viewport, this.canvas, this);
- masks[j].clip(this.context, viewport, this.canvas);
- }
- // Set the viewport transform
- if(!objects[i].ignoreViewport)
- {
- viewport.matrix.setContextTransform(this.context);
- }
- else if(masks.length > 0)
- {
- this.context.setTransform(1, 0, 0, 1, 0, 0);
- }
- // Apply the object transform to the canvas context
- objects[i].transform(this.context, viewport, this.canvas, this);
- // Style the canvas context
- if(objects[i].style !== null)
- {
- objects[i].style(this.context, viewport, this.canvas);
- }
- // Draw content into the canvas.
- if(objects[i].draw !== null)
- {
- objects[i].draw(this.context, viewport, this.canvas);
- }
- if(objects[i].restoreContextState)
- {
- this.context.restore();
- }
- }
- };
- export {Renderer};
- </code></pre>
- </article>
- </section>
- </div>
- <nav>
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</a></li><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="ColorStyle.html">ColorStyle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="LinearGradientStyle.html">LinearGradientStyle</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul><h3>Global</h3><ul><li><a href="global.html#writeFile">writeFile</a></li></ul>
- </nav>
- <br class="clear">
- <footer>
- Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
- </footer>
- <script> prettyPrint(); </script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|