1
0
Fork 0
Browse Source

feat: explicitely colour boxes or define on creation

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
combat-scorched-earth-from-outer-space
André Jaenisch 9 months ago
parent
commit
e82734dc03
Signed by: ryuno-ki
GPG Key ID: 5A668E771F1ED854
  1. 4
      src/js/draw.js
  2. 4
      src/js/shape.js
  3. 4
      src/js/world.js
  4. 1
      test/js/draw.test.js
  5. 6
      types/shape.d.ts

4
src/js/draw.js

@ -33,6 +33,8 @@ function prepareCanvas (context, shape) {
* @param {Shape} shape
*/
function draw (context, shape) {
const { W, H } = shape
const { c, H, W } = shape
context.strokeRect(-W / 2, -H / 2, W, H)
context.fillStyle = c
context.fillRect(-W / 2, -H / 2, W * 0.8, H * 0.8)
}

4
src/js/shape.js

@ -5,6 +5,7 @@ import { normalize, subtract, Vec2 } from './vector.js'
/**
* @typedef {object} Shape
* @property {Vector2D} C
* @property {string} c
* @property {number} F
* @property {number} R
* @property {number} M
@ -24,6 +25,7 @@ import { normalize, subtract, Vec2 } from './vector.js'
*
* @param {object} config
* @param {Vector2D} config.center
* @param {string} [config.color]
* @param {number} config.mass
* @param {number} config.friction
* @param {number} config.restitution
@ -34,6 +36,7 @@ import { normalize, subtract, Vec2 } from './vector.js'
*/
export function RigidShape ({
center,
color,
mass,
friction,
restitution, // bouncing
@ -52,6 +55,7 @@ export function RigidShape ({
G: 0, // angle
v: 0, // angle velocity
a: 0, // angle acceleration
c: color || `hsl(${~~(Math.random() * 360)}, 80%, 80%)`,
B: bounds,
W: width,
H: height,

4
src/js/world.js

@ -10,6 +10,7 @@ import { Vec2 } from './vector.js'
*/
export function makeAstronaut () {
const center = Vec2(200, 200)
const color = 'white'
const friction = 20
const restitution = 0
const mass = 400
@ -19,6 +20,7 @@ export function makeAstronaut () {
const shape = RigidShape({
center,
color,
mass,
friction,
restitution,
@ -42,6 +44,7 @@ export function makeAstronaut () {
*/
export function makeBoundary ({ x, y, height, width }) {
const center = Vec2(x + width / 2, y + height / 2)
const color = 'black'
const friction = 20
const restitution = 0
const mass = 0
@ -49,6 +52,7 @@ export function makeBoundary ({ x, y, height, width }) {
const shape = RigidShape({
center,
color,
mass,
friction,
restitution,

1
test/js/draw.test.js

@ -41,6 +41,7 @@ describe('drawShape', function () {
function makeContext () {
return {
fillRect: function () {},
restore: function () {},
rotate: function () {},
save: function () {},

6
types/shape.d.ts vendored

@ -3,6 +3,7 @@
/**
* @typedef {object} Shape
* @property {Vector2D} C
* @property {string} c
* @property {number} F
* @property {number} R
* @property {number} M
@ -21,6 +22,7 @@
*
* @param {object} config
* @param {Vector2D} config.center
* @param {string} [config.color]
* @param {number} config.mass
* @param {number} config.friction
* @param {number} config.restitution
@ -29,8 +31,9 @@
* @param {number} config.height
* @returns {Shape}
*/
export function RigidShape({ center, mass, friction, restitution, bounds, width, height }: {
export function RigidShape({ center, color, mass, friction, restitution, bounds, width, height }: {
center: Vector2D;
color?: string | undefined;
mass: number;
friction: number;
restitution: number;
@ -50,6 +53,7 @@ export function computeNormals(shape: Shape): void;
export type Vector2D = import('./vector.js').Vector2D;
export type Shape = {
C: Vector2D;
c: string;
F: number;
R: number;
M: number;

Loading…
Cancel
Save