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

Method draw

packages/melonjs/src/renderable/imagelayer.js:268–339  ·  view source on GitHub ↗

* draw this ImageLayer (automatically called by melonJS) * @protected * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer instance * @param {Camera2d} [viewport] - the viewport to (re)draw

(renderer, viewport)

Source from the content-addressed store, hash-verified

266 * @param {Camera2d} [viewport] - the viewport to (re)draw
267 */
268 draw(renderer, viewport) {
269 const width = this.width;
270 const height = this.height;
271 const bw = viewport.bounds.width;
272 const bh = viewport.bounds.height;
273 const ax = this.anchorPoint.x;
274 const ay = this.anchorPoint.y;
275 const rx = this.ratio.x;
276 const ry = this.ratio.y;
277 const vZoom = viewport.zoom;
278
279 let x, y;
280
281 if (rx === 0 && ry === 0) {
282 // static image
283 x = this.pos.x + ax * (bw - width);
284 y = this.pos.y + ay * (bh - height);
285 } else {
286 // parallax — compute position from the current viewport passed to draw()
287 x =
288 ax * (rx - 1) * (bw - viewport.width) +
289 this.offset.x -
290 rx * viewport.pos.x;
291 y =
292 ay * (ry - 1) * (bh - viewport.height) +
293 this.offset.y -
294 ry * viewport.pos.y;
295
296 if (this.repeatX) {
297 x = x % width;
298 }
299 if (this.repeatY) {
300 y = y % height;
301 }
302 }
303
304 // for zoomed cameras, scale positions and pattern to match the world view
305 renderer.translate(x * vZoom, y * vZoom);
306 renderer.scale(vZoom, vZoom);
307
308 // apply flip — pivot at the centre of the drawn pattern in the
309 // post-zoom local frame, so the mirror lands in the visible viewport
310 // regardless of `viewport.zoom`. Done here (not in preDraw) because
311 // preDraw runs before this translate/scale and would misalign.
312 if (this._flip.x || this._flip.y) {
313 const px = viewport.width;
314 const py = viewport.height;
315 renderer.translate(px, py);
316 renderer.scale(this._flip.x ? -1 : 1, this._flip.y ? -1 : 1);
317 renderer.translate(-px, -py);
318 }
319
320 // stencil mask — also applied in the post-zoom local frame so the
321 // mask shape's coordinates are interpreted relative to the layer's
322 // drawn area, matching `drawPattern` below.
323 if (this.mask) {
324 renderer.setMask(this.mask);
325 }

Callers

nothing calls this directly

Calls 4

translateMethod · 0.45
scaleMethod · 0.45
setMaskMethod · 0.45
drawPatternMethod · 0.45

Tested by

no test coverage detected