Skip to content

Commit

Permalink
options -> obj
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinten committed Aug 20, 2024
1 parent a296ffe commit 1f2e783
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ https://en.wikipedia.org/wiki/Web_colors
- 4269 / 13312
- 4267 / 13312
- 4315 / 13312
- 4313 / 13312
16 changes: 8 additions & 8 deletions game/js/draw/path.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default (options = {}) => {
export default (obj = {}) => {
let ow = 0; // original width
let oh = 0; // original height
let defaults = {
Expand All @@ -10,13 +10,13 @@ export default (options = {}) => {
h: 40,
a: 0, // 0-360
};
Object.assign(defaults, options);
Object.assign(options, defaults);
ow = options.w;
oh = options.h;
options.draw = e => {
Object.assign(defaults, obj);
Object.assign(obj, defaults);
ow = obj.w;
oh = obj.h;
obj.draw = e => {
let { ctx } = e;
let { paths, fills, x, y, w, h, a } = options;
let { paths, fills, x, y, w, h, a } = obj;
let rad = a * Math.PI / 180;
let cx = w / ow;
let cy = h / oh;
Expand All @@ -42,5 +42,5 @@ export default (options = {}) => {
ctx.fill(p);
});
};
return options;
return obj;
};
18 changes: 9 additions & 9 deletions game/js/draw/text.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default (options) => {
export default (obj = {}) => {
let defaults = {
text: '',
x: 16,
Expand All @@ -8,24 +8,24 @@ export default (options) => {
w: 0,
h: 24
};
Object.assign(defaults, options);
Object.assign(options, defaults);
options.draw = e => {
Object.assign(defaults, obj);
Object.assign(obj, defaults);
obj.draw = e => {
let { ctx } = e;
let { text, x, y, font, lineHeight } = options;
let { text, x, y, font, lineHeight } = obj;
ctx.font = font;
ctx.textBaseline = 'top';
let fontSize = font.match(/\d+/g);
fontSize = fontSize ? fontSize[0] : 16;
fontSize = Number(fontSize);
let lines = text.split('\n');
options.h = lines.length * fontSize * lineHeight;
options.w = 0;
obj.h = lines.length * fontSize * lineHeight;
obj.w = 0;
lines.forEach((line, i) => {
let w = ctx.measureText(line).width;
ctx.fillText(line, x, y + i * fontSize * lineHeight + fontSize * (lineHeight - 1) / 2);
options.w = Math.max(options.w, w);
obj.w = Math.max(obj.w, w);
});
};
return options;
return obj;
};
46 changes: 23 additions & 23 deletions game/js/pointer/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ css`body.p canvas{cursor:pointer;}`;

import events from '../events.js';

export default (options = {}) => {
export default (obj = {}) => {
let defaults = {
x: 0,
y: 0,
w: 40,
h: 40
};
Object.assign(defaults, options);
Object.assign(options, defaults);
Object.assign(defaults, obj);
Object.assign(obj, defaults);
let {on, off, once, emit, last} = events();
options.pointer = {
obj.pointer = {
on,
off,
once,
Expand All @@ -24,47 +24,47 @@ export default (options = {}) => {
};
window.addEventListener('pointerdown', e => {
let { clientX: x, clientY: y } = e;
if (x > options.x &&
x < options.x + options.w &&
y > options.y &&
y < options.y + options.h
if (x > obj.x &&
x < obj.x + obj.w &&
y > obj.y &&
y < obj.y + obj.h
) {
emit('down', {x, y});
options.pointer.down = true;
obj.pointer.down = true;
}
});
window.addEventListener('pointerup', e => {
let { clientX: x, clientY: y } = e;
if (x > options.x &&
x < options.x + options.w &&
y > options.y &&
y < options.y + options.h
if (x > obj.x &&
x < obj.x + obj.w &&
y > obj.y &&
y < obj.y + obj.h
) {
emit('tap', {x, y});
emit('up', {x, y});
}
options.pointer.down = false;
obj.pointer.down = false;
});
window.addEventListener('pointermove', e => {
let { clientX: x, clientY: y } = e;
if (x > options.x &&
x < options.x + options.w &&
y > options.y &&
y < options.y + options.h
if (x > obj.x &&
x < obj.x + obj.w &&
y > obj.y &&
y < obj.y + obj.h
) {
if (!options.pointer.pointing) {
if (!obj.pointer.pointing) {
emit('startpointing', {x, y});
document.body.classList.add('p');
}
emit('move', {x, y});
options.pointer.pointing = true;
obj.pointer.pointing = true;
} else {
if (options.pointer.pointing) {
if (obj.pointer.pointing) {
emit('stoppointing', {x, y});
document.body.classList.remove('p');
}
options.pointer.pointing = false;
obj.pointer.pointing = false;
}
});
return options;
return obj;
};

0 comments on commit 1f2e783

Please sign in to comment.