Skip to content

Commit 2c73c37

Browse files
authored
Merge pull request #30 from Absulit/points_update
Points update
2 parents 6ff3e1d + cff0c8e commit 2c73c37

4 files changed

Lines changed: 7 additions & 142 deletions

File tree

src/main.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import frag0 from './renderpasses/renderpass0/frag.js';
44
import vert from './renderpasses/renderpass0/vert.js';
55
import * as dat from 'datgui';
66
import { Dexie } from 'https://unpkg.com/dexie/dist/modern/dexie.mjs';
7-
import { countImageColors, loadImage, readTags, sprite, strToCodes, strToImage } from './utils.js';
7+
import { countImageColors, readTags } from './utils.js';
88

99
/**
1010
* @type {Points}
@@ -14,7 +14,7 @@ const gui = new dat.GUI({ name: 'Points GUI' });
1414
const folderOptions = gui.addFolder('options');
1515
const folderControls = gui.addFolder('controls');
1616
const folderSongs = gui.addFolder('songs');
17-
const MAXCHARS = 30;
17+
const size = { x: 8, y: 22 }, offset = -32, atlasPath = 'src/img/inconsolata_regular_8x22.png';
1818

1919
let audio = null;
2020
let loop = false;
@@ -79,10 +79,7 @@ async function playSong(song) {
7979
audio && audio.pause() && (audio = null);
8080
audio = points.setAudio('audio', audioUrl, options.volume, false, false);
8181

82-
points.setUniform('numChars', title.length < MAXCHARS ? title.length : MAXCHARS);
83-
84-
const songNameImg = strToImage(title, atlas, size);
85-
await points.setTextureImage('songName', songNameImg);
82+
await points.setTextureString('songName', title, atlasPath, size, offset);
8683

8784
let artworkLoaded = 0;
8885
song?.artworkColors && points.setStorageMap('artworkColors', song?.artworkColors.flat());
@@ -191,11 +188,8 @@ async function onCompleteTags(result) {
191188
}
192189

193190
folderSongs.add(song, 'fn').name(song.name);
194-
points.setUniform('numChars', title.length < MAXCHARS ? title.length : MAXCHARS);
195-
// points.setStorageMap('chars', strToCodes(title));
196191

197-
const songNameImg = strToImage(title, atlas, size);
198-
await points.setTextureImage('songName', songNameImg);
192+
await points.setTextureString('songName', title, atlasPath, size, offset);
199193
}
200194

201195
async function onErrorTags(response) {
@@ -375,25 +369,13 @@ points.setUniform('showMessage', 1);
375369
points.setUniform('rand', 0);
376370
points.setUniform('progress', 0);
377371
points.setUniform('artworkLoaded', 0);
378-
// points.setUniform('somecolor', colors.color2, 'vec3f');
379-
points.setUniform('numChars', 12);
380372
// points.setStorageMap('chars', strToCodes('Gravity Pull'), 'array<f32>')// TODO: setStorageMap doesn't work with u32 wrong sized
381373
points.setStorageMap('artworkColors', Array(16).fill(1), 'array<vec4f>');
382374
points.setStorage('variables', 'Variables');
383-
await points.setTextureImage('font', './src/img/inconsolata_regular_8x22.png');
384-
385-
386-
387-
const size = { x: 8, y: 22 };
388-
const atlas = await loadImage('src/img/inconsolata_regular_8x22.png');
389-
const messageStringImg = strToImage('Select a song to Play', atlas, size);
390-
await points.setTextureImage('messageString', messageStringImg);
391-
392-
393375

394-
const songNameImg = strToImage('Gravity Pull', atlas, size);
395-
await points.setTextureImage('songName', songNameImg);
396376

377+
await points.setTextureString('messageString', 'Select a song to Play', atlasPath, size, offset);
378+
await points.setTextureString('songName', 'Gravity Pull', atlasPath, size, offset);
397379

398380
const renderPasses = [
399381
new RenderPass(vert, frag0, null),

src/utils.js

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
export function strToCodes(str) {
2-
return Array.from(str).map(char => char.charCodeAt(0))
3-
}
4-
51
export function readTags(song) {
62
return new Promise((resolve, reject) => {
73
jsmediatags.read(song.file, {
@@ -60,52 +56,3 @@ export async function countImageColors(src) {
6056

6157
});
6258
}
63-
64-
/**
65-
*
66-
* @param {Image} atlas Image atlas to parse
67-
* @param {CanvasRenderingContext2D} ctx Canvas context
68-
* @param {Number} index index in the atlas, so 0 is the first char
69-
* @param {Object} size cell dimensions
70-
* @param {Number} finalIndex final positional index in the canvas
71-
*/
72-
export function sprite(atlas, ctx, index, size, finalIndex) {
73-
const { width } = atlas;
74-
const numColumns = width / size.x
75-
76-
const x = index % numColumns;
77-
const y = Math.floor(index / numColumns);
78-
79-
ctx.drawImage(
80-
atlas,
81-
x * size.x,
82-
y * size.y,
83-
size.x,
84-
size.y,
85-
86-
size.x * finalIndex,
87-
0,
88-
89-
size.x,
90-
size.y);
91-
}
92-
93-
export function strToImage(str, atlasImg, size){
94-
const chars = strToCodes(str);
95-
const canvas = document.createElement('canvas');
96-
canvas.width = chars.length * size.x;
97-
canvas.height = size.y;
98-
const ctx = canvas.getContext('2d');
99-
100-
chars.forEach((c, i) => sprite(atlasImg, ctx, c - 32, size, i));
101-
return canvas.toDataURL('image/png');
102-
}
103-
104-
export async function loadImage(src) {
105-
return new Promise((resolve, reject) => {
106-
const img = new Image();
107-
img.src = src;
108-
img.onload = () => resolve(img);
109-
img.onerror = err => reject(err);
110-
});
111-
}

todo.md

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)