MCPcopy
hub / github.com/KilledByAPixel/LittleJS / drawRectGradient

Function drawRectGradient

src/engineDraw.js:377–429  ·  view source on GitHub ↗

Draw a rect centered on pos with a gradient from top to bottom * @param {Vector2} pos * @param {Vector2} [size=vec2(1)] * @param {Color} [colorTop=WHITE] * @param {Color} [colorBottom=CLEAR_WHITE] * @param {number} [angle] * @param {boolean} [useWebGL=glEnable] * @param {boolean}

(pos, size, colorTop=WHITE, colorBottom=CLEAR_WHITE, angle=0, useWebGL=glEnable, screenSpace=false, context)

Source from the content-addressed store, hash-verified

375 * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context]
376 * @memberof Draw */
377function drawRectGradient(pos, size, colorTop=WHITE, colorBottom=CLEAR_WHITE, angle=0, useWebGL=glEnable, screenSpace=false, context)
378{
379 ASSERT(isVector2(pos), 'pos must be a vec2');
380 ASSERT(isVector2(size), 'size must be a vec2');
381 ASSERT(isColor(colorTop) && isColor(colorBottom), 'color is invalid');
382 ASSERT(isNumber(angle), 'angle must be a number');
383 ASSERT(!context || !useWebGL, 'context only supported in canvas 2D mode');
384
385 if (useWebGL && glEnable)
386 {
387 ASSERT(!!glContext, 'WebGL is not enabled!');
388 if (screenSpace)
389 {
390 // convert to world space
391 pos = screenToWorld(pos);
392 size = size.scale(1/cameraScale);
393 angle += cameraAngle;
394 }
395 // build 4 corner points for the rectangle
396 const points = [], colors = [];
397 const halfSizeX = size.x/2, halfSizeY = size.y/2;
398 const colorTopInt = colorTop.rgbaInt();
399 const colorBottomInt = colorBottom.rgbaInt();
400 const c = cos(-angle), s = sin(-angle);
401 for (let i=4; i--;)
402 {
403 const x = i & 1 ? halfSizeX : -halfSizeX;
404 const y = i & 2 ? halfSizeY : -halfSizeY;
405 const rx = x * c - y * s;
406 const ry = x * s + y * c;
407 const color = i & 2 ? colorTopInt : colorBottomInt;
408 points.push(vec2(pos.x + rx, pos.y + ry));
409 colors.push(color);
410 }
411 glDrawColoredPoints(points, colors);
412 }
413 else
414 {
415 // normal canvas 2D rendering method (slower)
416 ++drawCount;
417 ++primitiveCount;
418 size = new Vector2(size.x, -size.y); // fix upside down sprites
419 drawCanvas2D(pos, size, angle, false, (context)=>
420 {
421 // if no tile info, use untextured rect
422 const gradient = context.createLinearGradient(0, -.5, 0, .5);
423 gradient.addColorStop(0, colorTop.toString());
424 gradient.addColorStop(1, colorBottom.toString());
425 context.fillStyle = gradient;
426 context.fillRect(-.5, -.5, 1, 1);
427 }, screenSpace, context);
428 }
429}
430
431/** Draw a texture tiled (wrapped) across a rectangle in world space.
432 * Useful for backgrounds, repeating patterns, and seamless fills.

Callers 2

gameRenderFunction · 0.85
gameRenderFunction · 0.85

Calls 11

isVector2Function · 0.85
isColorFunction · 0.85
isNumberFunction · 0.85
screenToWorldFunction · 0.85
vec2Function · 0.85
glDrawColoredPointsFunction · 0.85
drawCanvas2DFunction · 0.85
rgbaIntMethod · 0.80
ASSERTFunction · 0.70
scaleMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected