-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support dynamic font loading in as many situations as possible
(including in web workers). For CJS builds, utilize webpack's publicPath feature. In ESM builds, dynamic import() works natively.
- Loading branch information
Showing
22 changed files
with
339 additions
and
121 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
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<style> | ||
html { | ||
font: 16px 'Helvetica Neue', Arial, sans-serif; | ||
} | ||
body { | ||
margin: 20px; | ||
} | ||
canvas { | ||
width: 500px; | ||
height: 200px; | ||
border: 1px solid gray; | ||
display: block; | ||
margin-bottom: 10px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<p> | ||
The five scores below are rendered by web workers via | ||
<a href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas" target="_blank">OffscreenCanvas</a>. | ||
</p> | ||
<script type="module"> | ||
if (window.Worker) { | ||
const numScores = 5; | ||
|
||
const workers = []; | ||
const offscreenCanvases = []; | ||
for (let i = 0; i < numScores; i++) { | ||
document.body.insertAdjacentHTML( | ||
'beforeend', | ||
`<canvas id="outputCanvas${i}" width="1000" height="400"></canvas>` | ||
); | ||
|
||
// Test the standard bundle (vexflow.js) and also the minimal bundle with dynamically loaded fonts (vexflow-core.js). | ||
const workerJS = Math.random() > 0.5 ? 'worker-vexflow.js' : 'worker-vexflow-core.js'; | ||
|
||
const w = new Worker(workerJS); | ||
w.onmessage = function (e) { | ||
console.log(`Message from #${i} / ${workerJS}: [${e.data}]`); | ||
}; | ||
workers.push(w); | ||
const offscreen = document.getElementById('outputCanvas' + i).transferControlToOffscreen(); | ||
offscreenCanvases.push(offscreen); | ||
} | ||
|
||
for (let i = 0; i < numScores; i++) { | ||
const canvas = offscreenCanvases[i]; | ||
workers[i].postMessage({ canvas: canvas }, [canvas] /* transfer list */); | ||
} | ||
} else { | ||
alert("This browser doesn't support web workers."); | ||
} | ||
</script> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-disable */ | ||
|
||
// Load demos/worker/index.html in a | ||
|
||
// Web Workers have an importScripts() method that allows you to load scripts. importScripts(...) is similar to require(...) in Node.js. | ||
VEX_BASE_PATH = '../../build/cjs/'; | ||
importScripts(VEX_BASE_PATH + 'vexflow-core.js'); | ||
|
||
onmessage = function (e) { | ||
postMessage('VexFlow BUILD: ' + Vex.Flow.BUILD); | ||
|
||
const fonts = ['Bravura', 'Gonville', 'Petaluma']; | ||
const randomFont = fonts[Math.floor(Math.random() * fonts.length)]; | ||
Vex.Flow.setMusicFont(randomFont).then(() => { | ||
const { Stave, CanvasContext, BarlineType, StaveNote, Formatter } = Vex.Flow; | ||
|
||
const offscreenCanvas = e.data.canvas; | ||
const offscreenCtx = offscreenCanvas.getContext('2d'); | ||
const ctx = new CanvasContext(offscreenCtx); | ||
ctx.scale(3, 3); | ||
|
||
// Render to the OffscreenCavans. | ||
const stave = new Stave(15, 0, 300); | ||
stave.setEndBarType(BarlineType.END); | ||
stave.addClef('treble').setContext(ctx).draw(); | ||
|
||
function makeRandomNote(duration = '4') { | ||
const notes = ['c/4', 'd/4', 'e/4', 'f/4', 'g/4', 'a/4', 'b/4', 'c/5', 'd/5', 'e/5']; | ||
const randomNote = notes[Math.floor(Math.random() * notes.length)]; | ||
return new StaveNote({ keys: [randomNote], duration: duration, stem_direction: randomNote[2] === '5' ? -1 : +1 }); | ||
} | ||
|
||
const notes = [makeRandomNote(), makeRandomNote('8'), makeRandomNote(), makeRandomNote('8'), makeRandomNote()]; | ||
Formatter.FormatAndDraw(ctx, stave, notes); | ||
|
||
ctx.fillStyle = '#CC0088'; | ||
ctx.fillText(randomFont + ' – vexflow-core.js with dynamically loaded fonts.', 5, 15); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-disable */ | ||
|
||
// Load demos/worker/index.html in a | ||
|
||
// Web Workers have an importScripts() method that allows you to load scripts. importScripts(...) is similar to require(...) in Node.js. | ||
importScripts('../../build/cjs/vexflow.js'); | ||
|
||
onmessage = function (e) { | ||
const fonts = ['Bravura', 'Gonville', 'Petaluma']; | ||
const randomFont = fonts[Math.floor(Math.random() * fonts.length)]; | ||
Vex.Flow.setMusicFont(randomFont); | ||
|
||
const { Stave, CanvasContext, BarlineType, StaveNote, Formatter } = Vex.Flow; | ||
|
||
const offscreenCanvas = e.data.canvas; | ||
const offscreenCtx = offscreenCanvas.getContext('2d'); | ||
const ctx = new CanvasContext(offscreenCtx); | ||
ctx.scale(3, 3); | ||
|
||
// Render to the OffscreenCavans. | ||
const stave = new Stave(15, 0, 300); | ||
stave.setEndBarType(BarlineType.END); | ||
stave.addClef('treble').setContext(ctx).draw(); | ||
|
||
function makeRandomNote(duration = '4') { | ||
const notes = ['c/4', 'd/4', 'e/4', 'f/4', 'g/4', 'a/4', 'b/4', 'c/5', 'd/5', 'e/5']; | ||
const randomNote = notes[Math.floor(Math.random() * notes.length)]; | ||
return new StaveNote({ keys: [randomNote], duration: duration, stem_direction: randomNote[2] === '5' ? -1 : +1 }); | ||
} | ||
|
||
const notes = [makeRandomNote('8'), makeRandomNote(), makeRandomNote(), makeRandomNote('8'), makeRandomNote()]; | ||
Formatter.FormatAndDraw(ctx, stave, notes); | ||
|
||
ctx.fillStyle = '#0098BB'; | ||
ctx.fillText(randomFont + ' – vexflow.js bundled with all music fonts.', 5, 15); | ||
|
||
postMessage('VexFlow BUILD: ' + Vex.Flow.BUILD); | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.