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)
| 375 | * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context] |
| 376 | * @memberof Draw */ |
| 377 | function 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. |
no test coverage detected