Skip to content

Commit 8d7104a

Browse files
committed
initial commit v0.1.0
0 parents  commit 8d7104a

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
npm-debug.log*
2+
node_modules/
3+
4+
build/
5+
*.tgz
6+
7+
.DS_Store
8+
*~

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
npm-debug.log*
2+
node_modules/
3+
4+
*.tgz
5+
6+
.DS_Store
7+
*~

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2016 Anton Gilgur
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# trim-canvas
2+
3+
A tiny (< 100 LoC) library for trimming whitespace from a `canvas` element with
4+
no dependencies.
5+
6+
## Installation
7+
8+
`npm i -S trim-canvas`
9+
10+
## Usage
11+
12+
```
13+
import trimCanvas from 'trim-canvas'
14+
15+
let canvas = document.createElement('canvas')
16+
17+
// do some drawing on it ...
18+
19+
trimCanvas(canvas)
20+
// now the whitespace has been trimmed
21+
```
22+
23+
If you don't want to mess with your existing canvas, then simply clone the
24+
canvas element beforehand.
25+
26+
`trim-canvas` returns the canvas element for easy chaining.
27+
28+
## Credits
29+
30+
Credits go to [@efc](https://github.com/efc) for writing a quick version of this
31+
in [this issue](https://github.com/szimek/signature_pad/issues/49#issue-29108215)
32+
and to the original [StackOverflow Answer](http://stackoverflow.com/a/12178531/3431180)
33+
that was credited there.

index.es6

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
export default function trimCanvas (canvas) {
2+
let context = canvas.getContext('2d')
3+
4+
let imgWidth = canvas.width
5+
let imgHeight = canvas.height
6+
7+
let imgData = context.getImageData(0, 0, imgWidth, imgHeight).data
8+
9+
// get the corners of the relevant content (everything that's not white)
10+
let cropTop = scanY(true, imgWidth, imgHeight, imgData)
11+
let cropBottom = scanY(false, imgWidth, imgHeight, imgData)
12+
let cropLeft = scanX(true, imgWidth, imgHeight, imgData)
13+
let cropRight = scanX(false, imgWidth, imgHeight, imgData)
14+
15+
let cropXDiff = cropRight - cropLeft
16+
let cropYDiff = cropBottom - cropTop
17+
18+
// get the relevant data from the calculated coordinates
19+
let trimmedData = context.getImageData(cropLeft, cropTop, cropXDiff,
20+
cropYDiff)
21+
22+
// set the trimmed width and height
23+
canvas.width = cropXDiff
24+
canvas.height = cropYDiff
25+
// clear the canvas
26+
context.clearRect(0, 0, cropXDiff, cropYDiff)
27+
// place the trimmed data into the cleared canvas to create
28+
// a new, trimmed canvas
29+
context.putImageData(trimmedData, 0, 0)
30+
return canvas // for chaining
31+
}
32+
33+
// returns the RGBA values of an x, y coord of imgData
34+
function getRGBA (x, y, imgWidth, imgData) {
35+
return {
36+
red: imgData[(imgWidth * y + x) * 4],
37+
green: imgData[(imgWidth * y + x) * 4 + 1],
38+
blue: imgData[(imgWidth * y + x) * 4 + 2],
39+
alpha: imgData[(imgWidth * y + x) * 4 + 3]
40+
}
41+
}
42+
43+
function getAlpha (x, y, imgWidth, imgData) {
44+
return getRGBA(x, y, imgWidth, imgData).alpha
45+
}
46+
47+
// finds the first y coord in imgData that is not white
48+
function scanY (fromTop, imgWidth, imgHeight, imgData) {
49+
let offset = fromTop ? 1 : -1
50+
let firstCol = fromTop ? 0 : imgHeight - 1
51+
52+
// loop through each row
53+
for (var y = firstCol; fromTop ? (y < imgHeight) : (y > -1); y += offset) {
54+
// loop through each column
55+
for (var x = 0; x < imgWidth; x++) {
56+
// if not white, return col
57+
if (getAlpha(x, y, imgWidth, imgData)) {
58+
return y
59+
}
60+
}
61+
}
62+
63+
// the whole image is white already
64+
return null
65+
}
66+
67+
// finds the first x coord in imgData that is not white
68+
function scanX (fromLeft, imgWidth, imgHeight, imgData) {
69+
let offset = fromLeft ? 1 : -1
70+
let firstRow = fromLeft ? 0 : imgWidth - 1
71+
72+
// loop through each column
73+
for (var x = firstRow; fromLeft ? (x < imgWidth) : (x > -1); x += offset) {
74+
// loop through each row
75+
for (var y = 0; y < imgHeight; y++) {
76+
// if not white, return row
77+
if (getAlpha(x, y, imgWidth, imgData)) {
78+
return x
79+
}
80+
}
81+
}
82+
83+
// the whole image is white already
84+
return null
85+
}

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "trim-canvas",
3+
"version": "0.1.0",
4+
"description": "A tiny library for trimming whitespace from a `canvas` element.",
5+
"main": "build/index.js",
6+
"devDependencies": {
7+
"babel-core": "^6.0.14",
8+
"babel-loader": "^6.0.0",
9+
"babel-preset-es2015": "^6.6.0",
10+
"webpack": "^1.12.2"
11+
},
12+
"scripts": {
13+
"test": "echo \"Error: no test specified\" && exit 1",
14+
"watch": "webpack --watch -d",
15+
"produce": "webpack -p",
16+
"lint": "standard index.es6"
17+
},
18+
"keywords": [
19+
"canvas",
20+
"trim",
21+
"whitespace",
22+
"remove",
23+
"blanks",
24+
"removeBlanks",
25+
"remove-blanks"
26+
],
27+
"repository": {
28+
"type": "git",
29+
"url": "https://github.com/agilgur5/trim-canvas.git"
30+
},
31+
"author": "Anton Gilgur",
32+
"license": "Apache-2.0"
33+
}

webpack.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
entry: './index.es6',
3+
output: {
4+
path: './build',
5+
filename: 'index.js',
6+
library: 'trimCanvas',
7+
libraryTarget: 'umd'
8+
},
9+
module: {
10+
loaders: [{
11+
test: /\.es6$/, loader: 'babel-loader', query: {presets: ['es2015']}
12+
}]
13+
}
14+
}

0 commit comments

Comments
 (0)