Place image to alpha channels in canvas
I need to find top, left, max width and max height values of all transparent places in canvas.
http://jsfiddle.net/alperxx/t521jra4/ (fiddle)
I have 4 transparent place in canvas. but i can't detect true places. First alpha channel corners colour must be red. Next is: black, yellow and green. But my groups is not correct.
If I complete the groups truely, every uploading photos will place to alpha places.
My Processes are in comment.
function alphaRatio(ctx) {
var pixelData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
const data = pixelData.data;
const pixel_groups = ;
var total = 0;
for (let i = 0, dx = 0; dx < data.length; i++, dx = i << 2) {
if (data[dx + 3] == 0) {
//First, I'm catching all transparent pixels.
total++;
var x = (dx / 4) % ctx.canvas.width;
var y = ~~((dx / 4) / ctx.canvas.width);
// if pixel after a last grouped item add to in.
var found = false;
if (pixel_groups.length) {
for (im = 0; im < pixel_groups.length; im++) {
last_pixels = pixel_groups[im][pixel_groups[im].length - 1];
if (
last_pixels.x + 1 == x || last_pixels.x - 1 == x ||
last_pixels.y + 1 == y || last_pixels.y - 1 == y
) {
found = true;
pixel_groups[im].push({
x: x,
y: y
});
break;
}
}
}
if (!found) {
pixel_groups.push([{
x: x,
y: y
}]);
}
}
}
// i grouped all them
if (pixel_groups.length) {
console.debug(pixel_groups);
for (i = 0; i < pixel_groups.length; i++) {
var alphawidth = {};
var alphaheight = {};
for (im = 0; im < pixel_groups[i].length; im++) {
//now lets calculate first pixel left and top for width and height
if (typeof(alphawidth['min']) === 'undefined') {
alphawidth['min'] = pixel_groups[i][im].x;
alphawidth['max'] = pixel_groups[i][im].x;
} else {
if (pixel_groups[i][im].x < alphawidth['min']) {
alphawidth['min'] = pixel_groups[i][im].x;
}
if (pixel_groups[i][im].x > alphawidth['max']) {
alphawidth['max'] = pixel_groups[i][im].x;
}
}
if (typeof(alphaheight['min']) === 'undefined') {
alphaheight['min'] = pixel_groups[i][im].y;
alphaheight['max'] = pixel_groups[i][im].y;
} else {
if (pixel_groups[i][im].y < alphaheight['min']) {
alphaheight['min'] = pixel_groups[i][im].y;
}
if (pixel_groups[i][im].y > alphaheight['max']) {
alphaheight['max'] = pixel_groups[i][im].y;
}
}
}
// update group key for only x y w h
pixel_groups[i] = {
x: pixel_groups[i][0].x,
y: pixel_groups[i][0].y,
w: alphawidth['max'] - alphawidth['min'],
h: alphaheight['max'] - alphaheight['min']
};
}
// for test alpha places put a colour all corners
for (i = 0; i < pixel_groups.length; i++) {
var colour = ['red', 'black', 'yellow', 'green'];
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
}
return pixel_groups;
}
return false;
}
javascript canvas fabricjs alpha-transparency
add a comment |
I need to find top, left, max width and max height values of all transparent places in canvas.
http://jsfiddle.net/alperxx/t521jra4/ (fiddle)
I have 4 transparent place in canvas. but i can't detect true places. First alpha channel corners colour must be red. Next is: black, yellow and green. But my groups is not correct.
If I complete the groups truely, every uploading photos will place to alpha places.
My Processes are in comment.
function alphaRatio(ctx) {
var pixelData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
const data = pixelData.data;
const pixel_groups = ;
var total = 0;
for (let i = 0, dx = 0; dx < data.length; i++, dx = i << 2) {
if (data[dx + 3] == 0) {
//First, I'm catching all transparent pixels.
total++;
var x = (dx / 4) % ctx.canvas.width;
var y = ~~((dx / 4) / ctx.canvas.width);
// if pixel after a last grouped item add to in.
var found = false;
if (pixel_groups.length) {
for (im = 0; im < pixel_groups.length; im++) {
last_pixels = pixel_groups[im][pixel_groups[im].length - 1];
if (
last_pixels.x + 1 == x || last_pixels.x - 1 == x ||
last_pixels.y + 1 == y || last_pixels.y - 1 == y
) {
found = true;
pixel_groups[im].push({
x: x,
y: y
});
break;
}
}
}
if (!found) {
pixel_groups.push([{
x: x,
y: y
}]);
}
}
}
// i grouped all them
if (pixel_groups.length) {
console.debug(pixel_groups);
for (i = 0; i < pixel_groups.length; i++) {
var alphawidth = {};
var alphaheight = {};
for (im = 0; im < pixel_groups[i].length; im++) {
//now lets calculate first pixel left and top for width and height
if (typeof(alphawidth['min']) === 'undefined') {
alphawidth['min'] = pixel_groups[i][im].x;
alphawidth['max'] = pixel_groups[i][im].x;
} else {
if (pixel_groups[i][im].x < alphawidth['min']) {
alphawidth['min'] = pixel_groups[i][im].x;
}
if (pixel_groups[i][im].x > alphawidth['max']) {
alphawidth['max'] = pixel_groups[i][im].x;
}
}
if (typeof(alphaheight['min']) === 'undefined') {
alphaheight['min'] = pixel_groups[i][im].y;
alphaheight['max'] = pixel_groups[i][im].y;
} else {
if (pixel_groups[i][im].y < alphaheight['min']) {
alphaheight['min'] = pixel_groups[i][im].y;
}
if (pixel_groups[i][im].y > alphaheight['max']) {
alphaheight['max'] = pixel_groups[i][im].y;
}
}
}
// update group key for only x y w h
pixel_groups[i] = {
x: pixel_groups[i][0].x,
y: pixel_groups[i][0].y,
w: alphawidth['max'] - alphawidth['min'],
h: alphaheight['max'] - alphaheight['min']
};
}
// for test alpha places put a colour all corners
for (i = 0; i < pixel_groups.length; i++) {
var colour = ['red', 'black', 'yellow', 'green'];
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
}
return pixel_groups;
}
return false;
}
javascript canvas fabricjs alpha-transparency
add a comment |
I need to find top, left, max width and max height values of all transparent places in canvas.
http://jsfiddle.net/alperxx/t521jra4/ (fiddle)
I have 4 transparent place in canvas. but i can't detect true places. First alpha channel corners colour must be red. Next is: black, yellow and green. But my groups is not correct.
If I complete the groups truely, every uploading photos will place to alpha places.
My Processes are in comment.
function alphaRatio(ctx) {
var pixelData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
const data = pixelData.data;
const pixel_groups = ;
var total = 0;
for (let i = 0, dx = 0; dx < data.length; i++, dx = i << 2) {
if (data[dx + 3] == 0) {
//First, I'm catching all transparent pixels.
total++;
var x = (dx / 4) % ctx.canvas.width;
var y = ~~((dx / 4) / ctx.canvas.width);
// if pixel after a last grouped item add to in.
var found = false;
if (pixel_groups.length) {
for (im = 0; im < pixel_groups.length; im++) {
last_pixels = pixel_groups[im][pixel_groups[im].length - 1];
if (
last_pixels.x + 1 == x || last_pixels.x - 1 == x ||
last_pixels.y + 1 == y || last_pixels.y - 1 == y
) {
found = true;
pixel_groups[im].push({
x: x,
y: y
});
break;
}
}
}
if (!found) {
pixel_groups.push([{
x: x,
y: y
}]);
}
}
}
// i grouped all them
if (pixel_groups.length) {
console.debug(pixel_groups);
for (i = 0; i < pixel_groups.length; i++) {
var alphawidth = {};
var alphaheight = {};
for (im = 0; im < pixel_groups[i].length; im++) {
//now lets calculate first pixel left and top for width and height
if (typeof(alphawidth['min']) === 'undefined') {
alphawidth['min'] = pixel_groups[i][im].x;
alphawidth['max'] = pixel_groups[i][im].x;
} else {
if (pixel_groups[i][im].x < alphawidth['min']) {
alphawidth['min'] = pixel_groups[i][im].x;
}
if (pixel_groups[i][im].x > alphawidth['max']) {
alphawidth['max'] = pixel_groups[i][im].x;
}
}
if (typeof(alphaheight['min']) === 'undefined') {
alphaheight['min'] = pixel_groups[i][im].y;
alphaheight['max'] = pixel_groups[i][im].y;
} else {
if (pixel_groups[i][im].y < alphaheight['min']) {
alphaheight['min'] = pixel_groups[i][im].y;
}
if (pixel_groups[i][im].y > alphaheight['max']) {
alphaheight['max'] = pixel_groups[i][im].y;
}
}
}
// update group key for only x y w h
pixel_groups[i] = {
x: pixel_groups[i][0].x,
y: pixel_groups[i][0].y,
w: alphawidth['max'] - alphawidth['min'],
h: alphaheight['max'] - alphaheight['min']
};
}
// for test alpha places put a colour all corners
for (i = 0; i < pixel_groups.length; i++) {
var colour = ['red', 'black', 'yellow', 'green'];
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
}
return pixel_groups;
}
return false;
}
javascript canvas fabricjs alpha-transparency
I need to find top, left, max width and max height values of all transparent places in canvas.
http://jsfiddle.net/alperxx/t521jra4/ (fiddle)
I have 4 transparent place in canvas. but i can't detect true places. First alpha channel corners colour must be red. Next is: black, yellow and green. But my groups is not correct.
If I complete the groups truely, every uploading photos will place to alpha places.
My Processes are in comment.
function alphaRatio(ctx) {
var pixelData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
const data = pixelData.data;
const pixel_groups = ;
var total = 0;
for (let i = 0, dx = 0; dx < data.length; i++, dx = i << 2) {
if (data[dx + 3] == 0) {
//First, I'm catching all transparent pixels.
total++;
var x = (dx / 4) % ctx.canvas.width;
var y = ~~((dx / 4) / ctx.canvas.width);
// if pixel after a last grouped item add to in.
var found = false;
if (pixel_groups.length) {
for (im = 0; im < pixel_groups.length; im++) {
last_pixels = pixel_groups[im][pixel_groups[im].length - 1];
if (
last_pixels.x + 1 == x || last_pixels.x - 1 == x ||
last_pixels.y + 1 == y || last_pixels.y - 1 == y
) {
found = true;
pixel_groups[im].push({
x: x,
y: y
});
break;
}
}
}
if (!found) {
pixel_groups.push([{
x: x,
y: y
}]);
}
}
}
// i grouped all them
if (pixel_groups.length) {
console.debug(pixel_groups);
for (i = 0; i < pixel_groups.length; i++) {
var alphawidth = {};
var alphaheight = {};
for (im = 0; im < pixel_groups[i].length; im++) {
//now lets calculate first pixel left and top for width and height
if (typeof(alphawidth['min']) === 'undefined') {
alphawidth['min'] = pixel_groups[i][im].x;
alphawidth['max'] = pixel_groups[i][im].x;
} else {
if (pixel_groups[i][im].x < alphawidth['min']) {
alphawidth['min'] = pixel_groups[i][im].x;
}
if (pixel_groups[i][im].x > alphawidth['max']) {
alphawidth['max'] = pixel_groups[i][im].x;
}
}
if (typeof(alphaheight['min']) === 'undefined') {
alphaheight['min'] = pixel_groups[i][im].y;
alphaheight['max'] = pixel_groups[i][im].y;
} else {
if (pixel_groups[i][im].y < alphaheight['min']) {
alphaheight['min'] = pixel_groups[i][im].y;
}
if (pixel_groups[i][im].y > alphaheight['max']) {
alphaheight['max'] = pixel_groups[i][im].y;
}
}
}
// update group key for only x y w h
pixel_groups[i] = {
x: pixel_groups[i][0].x,
y: pixel_groups[i][0].y,
w: alphawidth['max'] - alphawidth['min'],
h: alphaheight['max'] - alphaheight['min']
};
}
// for test alpha places put a colour all corners
for (i = 0; i < pixel_groups.length; i++) {
var colour = ['red', 'black', 'yellow', 'green'];
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x + pixel_groups[i].w,
top: pixel_groups[i].y,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
canvas.add(new fabric.Circle({
left: pixel_groups[i].x,
top: pixel_groups[i].y + pixel_groups[i].h,
radius: 4,
fill: colour[i],
originX: 'center',
originY: 'center',
hasControls: false
}));
}
return pixel_groups;
}
return false;
}
javascript canvas fabricjs alpha-transparency
javascript canvas fabricjs alpha-transparency
edited Nov 28 '18 at 13:30
formedya
asked Nov 21 '18 at 19:53
formedyaformedya
1629
1629
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419584%2fplace-image-to-alpha-channels-in-canvas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419584%2fplace-image-to-alpha-channels-in-canvas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown