Fix randomness failing for some sketch components (not so singleton after all) [#18]

This commit is contained in:
David Evans 2018-01-06 00:42:25 +00:00
parent eaf4a956d4
commit 86e8b89a9c
4 changed files with 49 additions and 21 deletions

View File

@ -6394,6 +6394,19 @@ define('sequence/themes/Sketch',[
const FONT_FAMILY = '"' + FONT + '",cursive'; const FONT_FAMILY = '"' + FONT + '",cursive';
const LINE_HEIGHT = 1.5; const LINE_HEIGHT = 1.5;
function deepCopy(o) {
if(typeof o !== 'object' || !o) {
return o;
}
const r = {};
for(let k in o) {
if(o.hasOwnProperty(k)) {
r[k] = deepCopy(o[k]);
}
}
return r;
}
const SETTINGS = { const SETTINGS = {
titleMargin: 10, titleMargin: 10,
outerMargin: 5, outerMargin: 5,
@ -6644,10 +6657,12 @@ define('sequence/themes/Sketch',[
} }
reset() { reset() {
// Initial seed: 0x00010203 // Arbitrary random seed with roughly balanced 1s / 0s
for(let i = 0; i < 4; ++ i) { // (taken from running Math.random a few times)
this.s[i] = i; this.s[0] = 0x177E9C74;
} this.s[1] = 0xAE6FFDCE;
this.s[2] = 0x3CF4F32B;
this.s[3] = 0x46449F88;
} }
nextFloat() { nextFloat() {
@ -6684,14 +6699,13 @@ define('sequence/themes/Sketch',[
this.handedness = -1; this.handedness = -1;
} }
this.random = new Random(); this.random = new Random();
Object.assign(this, SETTINGS); Object.assign(this, deepCopy(SETTINGS));
// TODO: these mutate the global, not our copy this.notes = deepCopy(NOTES);
// (fine for now since we're a singleton for all practical purposes)
this.agentCap.cross.render = this.renderCross.bind(this); this.agentCap.cross.render = this.renderCross.bind(this);
this.agentCap.bar.render = this.renderBar.bind(this); this.agentCap.bar.render = this.renderBar.bind(this);
this.agentCap.box.boxRenderer = this.renderBox.bind(this); this.agentCap.box.boxRenderer = this.renderBox.bind(this);
NOTES.note.boxRenderer = this.renderNote.bind(this); this.notes.note.boxRenderer = this.renderNote.bind(this);
NOTES.state.boxRenderer = this.renderState.bind(this); this.notes.state.boxRenderer = this.renderState.bind(this);
} }
reset() { reset() {
@ -6906,7 +6920,7 @@ define('sequence/themes/Sketch',[
} }
getNote(type) { getNote(type) {
return NOTES[type]; return this.notes[type];
} }
drawAgentLine(container, {x, y0, y1, width, className}) { drawAgentLine(container, {x, y0, y1, width, className}) {

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -20,6 +20,19 @@ define([
const FONT_FAMILY = '"' + FONT + '",cursive'; const FONT_FAMILY = '"' + FONT + '",cursive';
const LINE_HEIGHT = 1.5; const LINE_HEIGHT = 1.5;
function deepCopy(o) {
if(typeof o !== 'object' || !o) {
return o;
}
const r = {};
for(let k in o) {
if(o.hasOwnProperty(k)) {
r[k] = deepCopy(o[k]);
}
}
return r;
}
const SETTINGS = { const SETTINGS = {
titleMargin: 10, titleMargin: 10,
outerMargin: 5, outerMargin: 5,
@ -270,10 +283,12 @@ define([
} }
reset() { reset() {
// Initial seed: 0x00010203 // Arbitrary random seed with roughly balanced 1s / 0s
for(let i = 0; i < 4; ++ i) { // (taken from running Math.random a few times)
this.s[i] = i; this.s[0] = 0x177E9C74;
} this.s[1] = 0xAE6FFDCE;
this.s[2] = 0x3CF4F32B;
this.s[3] = 0x46449F88;
} }
nextFloat() { nextFloat() {
@ -310,14 +325,13 @@ define([
this.handedness = -1; this.handedness = -1;
} }
this.random = new Random(); this.random = new Random();
Object.assign(this, SETTINGS); Object.assign(this, deepCopy(SETTINGS));
// TODO: these mutate the global, not our copy this.notes = deepCopy(NOTES);
// (fine for now since we're a singleton for all practical purposes)
this.agentCap.cross.render = this.renderCross.bind(this); this.agentCap.cross.render = this.renderCross.bind(this);
this.agentCap.bar.render = this.renderBar.bind(this); this.agentCap.bar.render = this.renderBar.bind(this);
this.agentCap.box.boxRenderer = this.renderBox.bind(this); this.agentCap.box.boxRenderer = this.renderBox.bind(this);
NOTES.note.boxRenderer = this.renderNote.bind(this); this.notes.note.boxRenderer = this.renderNote.bind(this);
NOTES.state.boxRenderer = this.renderState.bind(this); this.notes.state.boxRenderer = this.renderState.bind(this);
} }
reset() { reset() {
@ -532,7 +546,7 @@ define([
} }
getNote(type) { getNote(type) {
return NOTES[type]; return this.notes[type];
} }
drawAgentLine(container, {x, y0, y1, width, className}) { drawAgentLine(container, {x, y0, y1, width, className}) {