Skip to content

Instantly share code, notes, and snippets.

@t16h05008
t16h05008 / trim_canvas.js
Last active March 20, 2022 22:58 — forked from timdown/trim_canvas.js
Forked from: https://gist.github.com/timdown/021d9c8f2aabc7092df564996f5afbbf Returns a copy of a canvas element with surrounding transparent space removed. Added the option to add padding to the new canvas as a second parameter.
var trimCanvas = (function() {
function rowBlank(imageData, width, y) {
for (var x = 0; x < width; ++x) {
if (imageData.data[y * width * 4 + x * 4 + 3] !== 0) return false;
}
return true;
}
function columnBlank(imageData, width, x, top, bottom) {
for (var y = top; y < bottom; ++y) {