-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to put all canvas-related functions in one Renderer class
also enables customization of the `osc` method
Showing
4 changed files
with
69 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,43 @@ | ||
const DEFAULT_FILL = "#111111"; | ||
const DEFAULT_STROKE = "#11ff11"; | ||
const HLINE_COLOR = "#555555"; | ||
function _initCvs(ctx, width, height){ | ||
ctx.fillStyle = DEFAULT_FILL; | ||
ctx.strokeStyle = DEFAULT_STROKE; | ||
} | ||
function _primer(ctx, width, height){ | ||
ctx.fillRect(0,0,width,height); | ||
ctx.strokeStyle = HLINE_COLOR; | ||
ctx.beginPath(); | ||
ctx.moveTo(0, height / 2); | ||
ctx.lineTo(width, height / 2); | ||
ctx.stroke(); | ||
ctx.strokeStyle = DEFAULT_STROKE; | ||
} | ||
function _drawRawOsc(ctx,data,width,height){ | ||
ctx.beginPath(); | ||
for(let i=0; i < data.length; i++){ | ||
let x = i * (width * 1.0 / data.length); // need to fix x | ||
let v = data[i] / 128.0; | ||
let y = v * height / 2; | ||
if(i === 0) ctx.moveTo(x,y); | ||
else ctx.lineTo(x,y); | ||
class Renderer{ | ||
fillStyle = DEFAULT_FILL | ||
strokeStyle = DEFAULT_STROKE | ||
hlineColor = HLINE_COLOR | ||
constructor(canvasElement){ | ||
this.cvs = canvasElement; | ||
let {width = 300, height = 150} = this.cvs; | ||
this.width = width; | ||
this.height = height; | ||
this.cctx = this.cvs.getContext("2d"); | ||
} | ||
init(){ | ||
} | ||
primer(){ | ||
this.cctx.fillRect(0,0,this.width,this.height); | ||
this.cctx.strokeStyle = this.hlineColor; | ||
this.cctx.beginPath(); | ||
this.cctx.moveTo(0, this.height / 2); | ||
this.cctx.lineTo(this.width, this.height / 2); | ||
this.cctx.stroke(); | ||
this.cctx.strokeStyle = this.strokeStyle; | ||
} | ||
osc(data){ | ||
this.cctx.beginPath(); | ||
for(let i=0; i < data.length; i++){ | ||
let x = i * (this.width * 1.0 / data.length); // need to fix x | ||
let v = data[i] / 128.0; | ||
let y = v * this.height / 2; | ||
if(i === 0) this.cctx.moveTo(x,y); | ||
else this.cctx.lineTo(x,y); | ||
} | ||
this.cctx.stroke(); | ||
} | ||
reset(){ | ||
this.cctx.clearRect(0 , 0, this.width, this.height); | ||
} | ||
ctx.stroke(); | ||
} | ||
export { | ||
_initCvs, | ||
_primer, | ||
_drawRawOsc | ||
} | ||
Renderer | ||
} |