MCPcopy Index your code
hub / github.com/KilledByAPixel/LittleJS / drawRect

Method drawRect

plugins/uiSystem.js:317–361  ·  view source on GitHub ↗

Draw a rectangle to the UI context * @param {Vector2} pos * @param {Vector2} size * @param {Color} [color] * @param {number} [lineWidth] * @param {Color} [lineColor] * @param {number} [cornerRadius] * @param {Color} [gradientColor] * @param {Color} [

(pos, size, color=WHITE, lineWidth=0, lineColor=BLACK, cornerRadius=0, gradientColor, shadowColor=BLACK, shadowBlur=0, shadowOffset=vec2())

Source from the content-addressed store, hash-verified

315 * @param {number} [shadowBlur]
316 * @param {Color} [shadowOffset] */
317 drawRect(pos, size, color=WHITE, lineWidth=0, lineColor=BLACK, cornerRadius=0, gradientColor, shadowColor=BLACK, shadowBlur=0, shadowOffset=vec2())
318 {
319 ASSERT(isVector2(pos), 'pos must be a vec2');
320 ASSERT(isVector2(size), 'size must be a vec2');
321 ASSERT(isColor(color), 'color must be a color');
322 ASSERT(isNumber(lineWidth), 'lineWidth must be a number');
323 ASSERT(isColor(lineColor), 'lineColor must be a color');
324 ASSERT(isNumber(cornerRadius), 'cornerRadius must be a number');
325
326 const context = uiSystem.uiContext;
327 if (gradientColor)
328 {
329 const g = context.createLinearGradient(
330 pos.x, pos.y-size.y/2, pos.x, pos.y+size.y/2);
331 const c = color.toString();
332 g.addColorStop(0, c);
333 g.addColorStop(.5, gradientColor.toString());
334 g.addColorStop(1, c);
335 context.fillStyle = g;
336 }
337 else
338 context.fillStyle = color.toString();
339 if (shadowBlur || shadowOffset.x || shadowOffset.y)
340 if (shadowColor.a > 0)
341 {
342 // setup shadow
343 context.shadowColor = shadowColor.toString();
344 context.shadowBlur = shadowBlur;
345 context.shadowOffsetX = shadowOffset.x;
346 context.shadowOffsetY = shadowOffset.y;
347 }
348 context.beginPath();
349 if (cornerRadius && context['roundRect'])
350 context['roundRect'](pos.x-size.x/2, pos.y-size.y/2, size.x, size.y, cornerRadius);
351 else
352 context.rect(pos.x-size.x/2, pos.y-size.y/2, size.x, size.y);
353 context.fill();
354 context.shadowColor = '#0000';
355 if (lineWidth && lineColor.a > 0)
356 {
357 context.strokeStyle = lineColor.toString();
358 context.lineWidth = lineWidth;
359 context.stroke();
360 }
361 }
362
363 /** Draw a line to the UI context
364 * @param {Vector2} posA

Callers 4

showConfirmDialogMethod · 0.45
renderMethod · 0.45
renderDebugMethod · 0.45
renderMethod · 0.45

Calls 6

vec2Function · 0.85
isVector2Function · 0.85
isColorFunction · 0.85
isNumberFunction · 0.85
ASSERTFunction · 0.50
toStringMethod · 0.45

Tested by

no test coverage detected