Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h3>Demo</h3>
<section class="range-slider" id="quality">
<span class="range-values"></span>
<input value="30" min="0" max="100" step="1" type="range">
<input value="35" min="0" max="100" step="1" type="range">
<input value="90" min="0" max="100" step="1" type="range">
</section>
</div>
<div class="control clearfix">
Expand Down
77 changes: 36 additions & 41 deletions demo/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
/* Using query selector */
window.$ = document.querySelector.bind(document);

var inputImageData = null, // reference to input image data
outputImagedata = null;

/* Utility to convert file size in readable format */
function readableFileSize(bytes, si) {;
var thresh = si
Expand Down Expand Up @@ -50,6 +47,7 @@
return bytes.toFixed(1) + ' ' + units[u];
}


/* Utility to convert data url to blob */
function dataURLtoUint8(dataurl) {
var arr = dataurl.split(','),
Expand All @@ -63,18 +61,38 @@
return u8arr;
}

/* event handler for image picker */
var readURL = function(file) {
var dataUrlReader = new FileReader();
dataUrlReader.onload = function(e) {
$('#input-img').setAttribute('src', e.target.result);
$('#input-file-stats').innerHTML = 'Input image [' + readableFileSize(file.size) + ']';
$('#terminal').style.display = 'block';
$('#file-dropper').style.display = 'none';
inputImageData = dataURLtoUint8(e.target.result);
}
dataUrlReader.readAsDataURL(file);

/* event handler for image picker and drop */
var displayFile = function(file) {
$('#input-img').src = URL.createObjectURL(file);
$('#input-img').onload = function(e) { URL.revokeObjectURL(file) };
$('#input-file-stats').innerHTML = 'Input image [' + readableFileSize(file.size) + ']';
$('#terminal').style.display = 'block';
$('#file-dropper').style.display = 'none';

/* run the compress function on click */
$('#compress').onclick = function(e) {
e.preventDefault();
$('#console').style.display = 'block';
$('.compress-text').style.display = 'block';
$('#control-panel').style.display = 'none';
var reader = new FileReader();
reader.readAsDataURL(file)
reader.onload = function(e){

worker.postMessage({
type: "command",
arguments: {
'quality': getRangeSliderValue(),
'speed': '' + getSliderValue()
},
file: {
"name": "input.png",
"data": dataURLtoUint8(e.target.result)
}
});
};
}
}

function getRangeSliderValue() {
Expand All @@ -101,22 +119,11 @@
return slider.value;
}

/* Run the compression function */
function runIt(args) {
worker.postMessage({
type: "command",
arguments: args,
file: {
"name": "input.png",
"data": inputImageData
}
});
}

/* Prepare downoad link form blob */
function getDownloadLink(fileData, fileName) {
if (fileName.match(/\.jpeg|\.gif|\.jpg|\.png/)) {
var blob = new Blob([fileData]);
var blob = new Blob([fileData], {type: 'image/png'});
var src = window.URL.createObjectURL(blob);
return src;
}
Expand Down Expand Up @@ -163,7 +170,7 @@
alert('not a valid PNG image');
return;
}
readURL(this.files[0]);
displayFile(this.files[0]);
}
}

Expand All @@ -180,7 +187,7 @@
e.preventDefault();

var file = e.dataTransfer.files[0];
readURL(file);
displayFile(file);
};

/* init range slider */
Expand All @@ -204,25 +211,14 @@
$('#speed-control').oninput = getSliderValue;
$('#speed-control').oninput();

/* run the compress function */
$('#compress').onclick = function(e) {
e.preventDefault();
$('#console').style.display = 'block';
$('.compress-text').style.display = 'block';
$('#control-panel').style.display = 'none';
runIt({
'quality': getRangeSliderValue(),
'speed': '' + getSliderValue()
});
}

$('#reset').onclick = function(e) {
e.preventDefault();
$('#input-img').setAttribute('src', '');
$('#input-file-stats').innerHTML = '';
$('#terminal').style.display = 'none';
$('#file-dropper').style.display = 'block';
inputImageData = null;
$('#compress').onclick = null;
}

/* refresh entire state */
Expand All @@ -239,7 +235,6 @@
$('#file-dropper').style.display = 'block';
$('.refresh-container').style.display = 'none';
$('#control-panel').style.display = 'block';
inputImageData = null;

}

Expand Down
2 changes: 1 addition & 1 deletion demo/js/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions demo/js/pngquant.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ function pngquant(file, options, printF) {

/* Create command line options to passed using input `options` object */
for (var key in options) {
if (!options.hasOwnProperty(key)) continue;
/* Prepend -- to option key name */
if (options[key]) {
if (typeof options[key]=='string') {
args.push("--" + key);
args.push(options[key]);
}
Expand Down
2 changes: 1 addition & 1 deletion demo/js/pngquant.min.js

Large diffs are not rendered by default.

20 changes: 6 additions & 14 deletions demo/js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
/* Import pngquant.js synchronously */
importScripts('pngquant.min.js');

/* storing initial date*/
var now = Date.now;

/* function to pass progess update */
function print(text) {
postMessage({'type': 'stdout', 'data': text});
Expand All @@ -15,23 +12,18 @@
onmessage = function(event) {
var message = event.data;
if (message.type === "command") {
var Module = {
print: print,
printErr: print,
files: message.files || [],
arguments: message.arguments || []
};
var args = message.arguments;

postMessage({
'type': 'start',
'data': JSON.stringify(Module.arguments)
'data': JSON.stringify(args)
});

print('Received command: ' + JSON.stringify(Module.arguments));
print('Received command: ' + JSON.stringify(args));

var time = now();
var result = pngquant(message.file.data, message.arguments, print);
var totalTime = now() - time;
var time = performance.now();
var result = pngquant(message.file.data, args, print);
var totalTime = (performance.now() - time).toFixed(3);

print('Finished processing (took ' + totalTime + 'ms)');

Expand Down
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "pngquantjs",
"version": "1.0.0",
"description": "pngquant in JS using Emscripten",
"author": "Mayank Bhola <[email protected]>",
"license": "ISC",
"bugs": "https://github.com/psych0der/pngquantjs/issues",
"repository": "git+https://github.com/psych0der/pngquantjs.git",
"homepage": "https://github.com/psych0der/pngquantjs#readme",
"keywords": [
"png",
"pngquant",
"emscripten",
"image",
"img",
"compress",
"optimize"
],
"main": "dist/pngquant.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {},
"devDependencies": {}
}
4 changes: 1 addition & 3 deletions pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ function pngquant(file, options, printF) {

/* Create command line options to passed using input `options` object */
for (var key in options) {
if (!options.hasOwnProperty(key)) continue;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know about 'for in' reading iterable keys in prototype, but this won't happen here, it's a plain object given by user

/* Prepend -- to option key name */
if (options[key]) {
if (typeof options[key] == 'string') {
args.push("--" + key);
args.push(options[key]);
}
Expand Down