Skip to content

Commit 2e215ae

Browse files
committed
Implement DrawRectangleV, GetRandomValue, and ColorFromHSV
1 parent a71e743 commit 2e215ae

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

raylib.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ class RaylibJs {
184184
this.ctx.fillRect(posX, posY, width, height);
185185
}
186186

187+
DrawRectangleV(position_ptr, size_ptr, color_ptr) {
188+
const buffer = this.wasm.instance.exports.memory.buffer;
189+
const color = getColorFromMemory(buffer, color_ptr);
190+
const position = new Float32Array(buffer, position_ptr, 2);
191+
const size = new Float32Array(buffer, size_ptr, 2);
192+
this.ctx.fillStyle = color;
193+
this.ctx.fillRect(position[0], position[1], size[0], size[1]);
194+
}
195+
187196
IsKeyPressed(key) {
188197
return !this.prevPressedKeyState.has(key) && this.currentPressedKeyState.has(key);
189198
}
@@ -344,6 +353,41 @@ class RaylibJs {
344353
this.ctx.fillText(text, posX, posY + fontSize);
345354
}
346355

356+
GetRandomValue(min, max) {
357+
return min + Math.floor(Math.random()*(max - min + 1));
358+
}
359+
360+
ColorFromHSV(result_ptr, hue, saturation, value) {
361+
const buffer = this.wasm.instance.exports.memory.buffer;
362+
const result = new Uint8Array(buffer, result_ptr, 4);
363+
364+
// Red channel
365+
let k = (5.0 + hue/60.0)%6;
366+
let t = 4.0 - k;
367+
k = (t < k)? t : k;
368+
k = (k < 1)? k : 1;
369+
k = (k > 0)? k : 0;
370+
result[0] = Math.floor((value - value*saturation*k)*255.0);
371+
372+
// Green channel
373+
k = (3.0 + hue/60.0)%6;
374+
t = 4.0 - k;
375+
k = (t < k)? t : k;
376+
k = (k < 1)? k : 1;
377+
k = (k > 0)? k : 0;
378+
result[1] = Math.floor((value - value*saturation*k)*255.0);
379+
380+
// Blue channel
381+
k = (1.0 + hue/60.0)%6;
382+
t = 4.0 - k;
383+
k = (t < k)? t : k;
384+
k = (k < 1)? k : 1;
385+
k = (k > 0)? k : 0;
386+
result[2] = Math.floor((value - value*saturation*k)*255.0);
387+
388+
result[3] = 255;
389+
}
390+
347391
raylib_js_set_entry(entry) {
348392
this.entryFunction = this.wasm.instance.exports.__indirect_function_table.get(entry);
349393
}

0 commit comments

Comments
 (0)