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

Function drawPoly

src/engineDraw.js:634–671  ·  view source on GitHub ↗

Draw colored polygon using passed in points * @param {Array } points - Array of Vector2 points * @param {Color} [color=WHITE] * @param {number} [lineWidth] * @param {Color} [lineColor=BLACK] * @param {Vector2} [pos=vec2()] - Offset to apply * @param {number} [angle] - Angle

(points, color=WHITE, lineWidth=0, lineColor=BLACK, pos=vec2(), angle=0, useWebGL=glEnable, screenSpace=false, context=undefined)

Source from the content-addressed store, hash-verified

632 * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context]
633 * @memberof Draw */
634function drawPoly(points, color=WHITE, lineWidth=0, lineColor=BLACK, pos=vec2(), angle=0, useWebGL=glEnable, screenSpace=false, context=undefined)
635{
636 ASSERT(isVector2(pos), 'pos must be a vec2');
637 ASSERT(isArray(points), 'points must be an array');
638 ASSERT(isColor(color) && isColor(lineColor), 'color is invalid');
639 ASSERT(isNumber(lineWidth), 'lineWidth must be a number');
640 ASSERT(isNumber(angle), 'angle must be a number');
641 ASSERT(!context || !useWebGL, 'context only supported in canvas 2D mode');
642
643 if (useWebGL && glEnable)
644 {
645 ASSERT(!!glContext, 'WebGL is not enabled!');
646 let size = vec2(1);
647 if (screenSpace)
648 [pos, size, angle] = screenToWorldTransform(pos, size, angle);
649 glDrawPointsTransform(points, color.rgbaInt(), pos.x, pos.y, size.x, size.y, angle);
650 if (lineWidth > 0)
651 glDrawOutlineTransform(points, lineColor.rgbaInt(), lineWidth, pos.x, pos.y, size.x, size.y, angle);
652 }
653 else
654 {
655 drawCanvas2D(pos, vec2(1), angle, false, context=>
656 {
657 context.fillStyle = color.toString();
658 context.beginPath();
659 for (const point of points)
660 context.lineTo(point.x, point.y);
661 context.closePath();
662 context.fill();
663 if (lineWidth)
664 {
665 context.strokeStyle = lineColor.toString();
666 context.lineWidth = lineWidth;
667 context.stroke();
668 }
669 }, screenSpace, context);
670 }
671}
672
673/** Draw colored ellipse using passed in point
674 * @param {Vector2} pos

Callers 4

drawRegularPolyFunction · 0.85
drawFixtureMethod · 0.85
setupDebugDrawFunction · 0.85
gameRenderFunction · 0.85

Calls 12

vec2Function · 0.85
isVector2Function · 0.85
isArrayFunction · 0.85
isColorFunction · 0.85
isNumberFunction · 0.85
screenToWorldTransformFunction · 0.85
glDrawPointsTransformFunction · 0.85
glDrawOutlineTransformFunction · 0.85
drawCanvas2DFunction · 0.85
rgbaIntMethod · 0.80
ASSERTFunction · 0.70
toStringMethod · 0.45

Tested by

no test coverage detected