MCPcopy Create free account
hub / github.com/melonjs/melonJS / preDraw

Method preDraw

packages/melonjs/src/renderable/renderable.js:845–906  ·  view source on GitHub ↗

* Prepare the rendering context before drawing (automatically called by melonJS). * This will apply any defined transforms, anchor point, tint or blend mode and translate the context accordingly to this renderable position. * @see Renderable#draw * @see Renderable#postDraw * @param {CanvasRe

(renderer)

Source from the content-addressed store, hash-verified

843 * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer object
844 */
845 preDraw(renderer) {
846 const ax = this.width * this.anchorPoint.x;
847 const ay = this.height * this.anchorPoint.y;
848
849 // save renderer context
850 renderer.save();
851
852 // apply the defined alpha value
853 renderer.setGlobalAlpha(renderer.globalAlpha() * this.getOpacity());
854
855 // apply flip
856 if (this._flip.x || this._flip.y) {
857 const dx = this._flip.x ? this.centerX - ax : 0;
858 const dy = this._flip.y ? this.centerY - ay : 0;
859
860 renderer.translate(dx, dy);
861 renderer.scale(this._flip.x ? -1 : 1, this._flip.y ? -1 : 1);
862 renderer.translate(-dx, -dy);
863 }
864
865 // apply stencil mask if defined
866 if (this.mask) {
867 renderer.translate(this.pos.x, this.pos.y);
868 renderer.setMask(this.mask);
869 renderer.translate(-this.pos.x, -this.pos.y);
870 }
871
872 // delegate post-effect setup to the renderer.
873 // Camera manages its own FBO lifecycle in draw().
874 if (!this._postEffectManaged) {
875 renderer.beginPostEffect(this);
876 }
877
878 if (this.autoTransform === true && !this.currentTransform.isIdentity()) {
879 // apply the renderable transformation matrix
880 renderer.translate(this.pos.x, this.pos.y);
881 renderer.transform(this.currentTransform);
882 renderer.translate(-this.pos.x, -this.pos.y);
883 }
884
885 // offset by the anchor point — unless this renderable manages its own
886 // world placement (see Renderable#applyAnchorTransform), in which case
887 // the normalized anchor offset must not leak into the renderer/view
888 // transform.
889 if (this.applyAnchorTransform !== false) {
890 renderer.translate(-ax, -ay);
891 }
892
893 // apply the current tint and opacity
894 renderer.setTint(this.tint, this.getOpacity());
895
896 // forward depth to the renderer — pushed into the WebGL vertex
897 // stream as the `z` component by the batchers. No visible effect
898 // under the default ortho projection; required for perspective
899 // (Camera3d) to scale and parallax this renderable by distance.
900 renderer.setDepth(this.depth);
901
902 // apply blending if different from "normal"

Callers

nothing calls this directly

Calls 14

getOpacityMethod · 0.95
globalAlphaMethod · 0.80
setTintMethod · 0.80
setDepthMethod · 0.80
getBlendModeMethod · 0.80
saveMethod · 0.45
setGlobalAlphaMethod · 0.45
translateMethod · 0.45
scaleMethod · 0.45
setMaskMethod · 0.45
beginPostEffectMethod · 0.45
isIdentityMethod · 0.45

Tested by

no test coverage detected