Skip to content

Commit

Permalink
Make it work on node.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroldo de Oliveira Pinheiro committed Aug 9, 2023
2 parents b4c990e + 4a79eda commit f7fd1e4
Show file tree
Hide file tree
Showing 8 changed files with 1,192 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<script src="js/jquery-2.0.3.min.js"></script>
<script src="js/underscore-min.js"></script>
<script src="js/clusterfck.js"></script>
<script src="../src/rgbquant.js"></script>
<script src="../src/rgbquant-sms.js"></script>
<script src="js/clusterfck.js"></script>
<script src="js/helpers.js"></script>
<script src="js/preCode.js"></script>
<script src="js/timer.js"></script>
Expand Down
4 changes: 3 additions & 1 deletion demo/js/clusterfck.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,6 @@ function kmeans(points, k, distance, snapshotPeriod, snapshotCb) {
module.exports = kmeans;

});
return require('/clusterfck')})();
return require('/clusterfck')})();

this.clusterfck = clusterfck;
47 changes: 47 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const Canvas = require('canvas');
const Image = Canvas.Image;

const { RgbQuantSMS } = require('./src/rgbquant-sms');

console.log('Canvas', Canvas);

(async () => {

const quant = new RgbQuantSMS({
colors: 16,
paletteCount: 1,
maxTiles: 256,
minHueCols: 0,
weighPopularity: true
});

const getCanvas = async (src) => new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = new Canvas.Canvas(img.width, img.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, img.width, img.height);
canvas._typeWorkaround = 'Canvas';
resolve(canvas);
}
img.onerror = reject;
img.src = src;
});

const canvas = await getCanvas('demo/img/biking.jpg');
console.log('canvas', canvas);

quant.sample(canvas);
const palettes = quant.palettes();
console.log('palettes', palettes)

const unoptimizedTileMap = quant.reduceToTileMap(canvas);
const optimizedTileMap = quant.normalizeTiles(unoptimizedTileMap);
quant.updateTileEntropy(optimizedTileMap.tiles);
const similarTiles = quant.groupBySimilarity(optimizedTileMap);
const reducedTileMap = quant.removeSimilarTiles(optimizedTileMap, similarTiles);

console.log('reducedTileMap', { reducedTileMap, tileCount: reducedTileMap.tiles.length });
})();
Loading

0 comments on commit f7fd1e4

Please sign in to comment.