Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // functions to copy just the part with non-transparent pixels
- // https://gist.github.com/timdown/021d9c8f2aabc7092df564996f5afbbf
- function rowBlank(imageData, width2, y) {
- for (var x = 0; x < width2; ++x) {
- if (imageData[y * width2 * 4 + x * 4 + 3] !== 0) return false;
- }
- return true;
- }
- function columnBlank(imageData, width2, x, top, bottom) {
- for (var y = top; y < bottom; ++y) {
- if (imageData[y * width2 * 4 + x * 4 + 3] !== 0) return false;
- }
- return true;
- }
- function trimCanvas(c) {
- var ctx = c;
- var width2 = c.width;
- ctx.loadPixels();
- var top = 0, bottom = c.height, left = 0, right = width2;
- while (top < bottom && rowBlank(ctx.pixels, width2, top)) ++top;
- while (bottom - 1 > top && rowBlank(ctx.pixels, width2, bottom - 1)) --bottom;
- while (left < right && columnBlank(ctx.pixels, width2, left, top, bottom)) ++left;
- while (right - 1 > left && columnBlank(ctx.pixels, width2, right - 1, top, bottom)) --right;
- var trimmedWidth = right - left , trimmedHeight = bottom - top;
- var trimmed = ctx.get(left, top, trimmedWidth,trimmedHeight);
- image(trimmed,0,0, trimmed.width*.66, trimmed.height*.66);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement