Skip to content

Commit

Permalink
web worker js code refactoring
Browse files Browse the repository at this point in the history
update example
add React App example
  • Loading branch information
Donaldcwl committed Mar 16, 2020
1 parent a99d444 commit fbbbadb
Show file tree
Hide file tree
Showing 22 changed files with 11,142 additions and 52 deletions.
2 changes: 1 addition & 1 deletion dist/browser-image-compression.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser-image-compression.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser-image-compression.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser-image-compression.mjs.map

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions example/React App/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "my-image-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-scripts": "3.4.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added example/React App/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions example/React App/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added example/React App/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/React App/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions example/React App/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions example/React App/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
19 changes: 19 additions & 0 deletions example/React App/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.App {
font-family: sans-serif;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}

td {
vertical-align: top;
width: 50%;
}

img {
max-width: 100%;
}
153 changes: 153 additions & 0 deletions example/React App/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import React from 'react'
import './App.css'
import imageCompression from 'browser-image-compression'

export default class App extends React.Component {
constructor (...args) {
super(...args)
this.compressImage = this.compressImage.bind(this)
this.handleChange = this.handleChange.bind(this)
this.state = {
maxSizeMB: 1,
maxWidthOrHeight: 1024,
webWorker: {
progress: null,
inputSize: null,
outputSize: null,
inputUrl: null,
outputUrl: null
},
mainThread: {
progress: null,
inputSize: null,
outputSize: null,
inputUrl: null,
outputUrl: null
}
}
}

handleChange (target) {
return (e) => {
this.setState({ [target]: e.currentTarget.value })
}
}

onProgress (p, useWebWorker) {
const targetName = useWebWorker ? 'webWorker' : 'mainThread'
this.setState(prevState => ({
...prevState,
[targetName]: {
...prevState[targetName],
progress: p
}
}))
}

async compressImage (event, useWebWorker) {
const file = event.target.files[0]
console.log('input', file)
console.log(
'ExifOrientation',
await imageCompression.getExifOrientation(file)
)
const targetName = useWebWorker ? 'webWorker' : 'mainThread'
this.setState(prevState => ({
...prevState,
[targetName]: {
...prevState[targetName],
inputSize: (file.size / 1024 / 1024).toFixed(2),
inputUrl: URL.createObjectURL(file)
}
}))
var options = {
maxSizeMB: this.state.maxSizeMB,
maxWidthOrHeight: this.state.maxWidthOrHeight,
useWebWorker,
onProgress: p => this.onProgress(p, useWebWorker)
}
const output = await imageCompression(file, options)
console.log('output', output)
this.setState(prevState => ({
...prevState,
[targetName]: {
...prevState[targetName],
outputSize: (output.size / 1024 / 1024).toFixed(2),
outputUrl: URL.createObjectURL(output)
}
}))
}

render () {
const { webWorker, mainThread, maxSizeMB, maxWidthOrHeight } = this.state
return (
<div className="App">
<div>
Options:<br />
<label htmlFor="maxSizeMB">maxSizeMB: <input type="number" id="maxSizeMB" name="maxSizeMB"
value={maxSizeMB}
onChange={this.handleChange('maxSizeMB')} /></label><br />
<label htmlFor="maxWidthOrHeight">maxWidthOrHeight: <input type="number" id="maxWidthOrHeight"
name="maxWidthOrHeight"
value={maxWidthOrHeight}
onChange={this.handleChange('maxWidthOrHeight')} /></label>
<hr />
<label htmlFor="web-worker">
Compress in web-worker{' '}
{webWorker.progress && <span>{webWorker.progress} %</span>}
<input
id="web-worker"
type="file"
accept="image/*"
onChange={e => this.compressImage(e, true)}
/>
</label>
<p>
{webWorker.inputSize && (
<span>Source image size: {webWorker.inputSize} mb</span>
)}
{webWorker.outputSize && (
<span>, Output image size: {webWorker.outputSize}</span>
)}
</p>
</div>
<div>
<label htmlFor="main-thread">
Compress in main-thread{' '}
{mainThread.progress && <span>{mainThread.progress} %</span>}
<input
id="main-thread"
type="file"
accept="image/*"
onChange={e => this.compressImage(e, false)}
/>
</label>
<p>
{mainThread.inputSize && (
<span>Source image size: {mainThread.inputSize} mb</span>
)}
{mainThread.outputSize && (
<span>, Output image size: {mainThread.outputSize}</span>
)}
</p>
</div>
{(mainThread.inputUrl || webWorker.inputUrl) && (
<table>
<thead>
<tr>
<td>input preview</td>
<td>output preview</td>
</tr>
</thead>
<tbody>
<tr>
<td><img src={mainThread.inputUrl || webWorker.inputUrl} alt="input" /></td>
<td><img src={mainThread.outputUrl || webWorker.outputUrl} alt="output" /></td>
</tr>
</tbody>
</table>
)}
</div>
)
}
};
13 changes: 13 additions & 0 deletions example/React App/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
12 changes: 12 additions & 0 deletions example/React App/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
7 changes: 7 additions & 0 deletions example/React App/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fbbbadb

Please sign in to comment.