Cropping a image on a canvas - javascript

I have 2 canvases side by side , and in the first one I have resizable draggable div as a crop region and I want to display the cropped region on the second canvas . But it is not happening in my case. Any help is appreciated
function crop(){
var x = $("#crop_square").width();
var y = $("#crop_square").height();
var ty = $("#crop_square").offset().top -
$("#area_c").offset().top;
var tx = $("#crop_square").offset().left -
$("#area_c").offset().left;
alert(x);
alert(y);
alert(ty);
alert(tx);
var c = document.getElementById("area_c"); //canvaas1
var c2 = document.getElementById("area_c2"); // canvas2
var ctx2 = c2.getContext("2d");
ctx2.drawImage(c,tx,ty,x,y,0,0,x,y );
}

If you need to "crop" a canvas with a div you may use the div's BoundingClientRect data to draw the image on the other canvas:
I hope this is what you were asking.
let cw = (area_c.width = area_c2.width = 400);
let ch = (area_c.height = area_c2.height = 400);
const ctx1 = area_c.getContext("2d");
const ctx2 = area_c2.getContext("2d");
theImg.onload = function() {
ctx1.drawImage(theImg, 0, 0);
let bcr = crop_square.getBoundingClientRect();
ctx2.drawImage(
theImg,
bcr.x,
bcr.y,
bcr.width,
bcr.height,
bcr.x,
bcr.y,
bcr.width,
bcr.height
);
}
.wrap {
width: 300px;
height: 300px;
border: 1px solid;
display: inline-block;
position: relative;
}
#crop_square {
position: absolute;
width: 150px;
height: 75px;
left: 100px;
top: 53px;
border: 5px solid gold;
}
<div class="wrap">
<canvas id="area_c">
<img id="theImg" src="data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAZAAD/4QMBaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjMtYzAxMSA2Ni4xNDU2NjEsIDIwMTIvMDIvMDYtMTQ6NTY6MjcgICAgICAgIC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FqY8WOIaIq1FUxZBZnddHkzJnjIaUJbX6jRMsBzcceRj27tkoGh6SU3FEhggN24gMfFVOUJY5HkweQAbNHVhaYU3t2UZZTIrEgFFEXNfFcwFrJ9kSToT8Fwx1/FSaCeIZBKJA6gqjPi5A9LarUMNRYpTE0aoHXonKGHMCQOPJHdHofkxUBlk45q88J/X+qH+7r4rX5HGf1Q8WVCUMg0VZTgsSCxBcGoIXEAqI4p4zuwihviNj/t6KbHE5YGYBYHbIG8ZftKYGLAOfJOJiHqkaCvkhHHKxCSGGXIzbb4sZ9XSUunkkGpw4SkPv5QxmPRH9sP7lXWht7KLDSDH/AKqRqss1q4ALXRF3RNkAzfimRqEsdEKsdqMfkhWjW1QCiwf4IvESIa9XRq5v3QGyj3ZIzC4apHVdLumDdu6X9Nfm1kwMUdj1JQH+ln06IxsEyBqNqkiHJc2LfBShqpJXDefRIwJrSx1UOWLF3d+qsV/s6gyuwfqfgj+iVpKrlNVZltel9FXltdVCQShKUS3khxsEs8+kI/V/ZXIbW7aruPt/lH7X0t6+nbzTv8BejjiAHHl0TCIs1EwsOi437aLPaiSxxUeTBGUWr1BVhdGyAoCOfE/o3f6hVQyzZpZ4x2HfKjLWG1ze6Po7bvn5JzP6Jl5IcogyjAv0e6vcDjjDhjOUNmSQ9f5KUfVTr8lOHetu6NmAf+tEfCoQ1qmigFD06JJemXink9UmT6a3aiQRm1KpZ44SFRXqiNHQ1HyTCGXHh49EDxxWeOWzIQ268ZR/bOOoUvi/dE7WDI2WlTJmiSOOYDFmkQA5eDfujJWsePHjAjji0RTuq0PsbMn8tt+7/l3dP07U3C+969277H/q3/8Albv28ap/gL8C1wpNPN1DV43fVlNp3Uh//9k="/>
</canvas>
<div id="crop_square"></div>
</div>
<div class="wrap">
<canvas id="area_c2"></canvas>
</div>

Related

How to Handle Large image in Jcrop?

I am using Jcrop to crop Images, it is working fine with small size images, but Whenever I try to put large image like 2080x2080 or 1600x1600 it covers all my screen, also It cannot be handle with CSS like width and height control in Image tag
Seeking Solution/Suggestion -
Any method to put this image in container/div and then zoom-in/out with mouse event ?
Any method to handle large size image in a fix container and crop so that image maintain it's quality.
P.s: you can try with large image in below working example and see how it behaves with big Image.
var jcp;
var save = document.getElementById('save');
var imageLoader = document.getElementById('imageLoader');
var img = document.getElementById("target");
imageLoader.onchange = function handleImage(e) { //handling our image picker <input>:
var reader = new FileReader();
reader.onload = function(event) {
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
save.onclick = function() {
if (jcp && jcp.active) {
var i = 0;
for (area of jcp.crops) {
i++;
canvas = document.createElement("canvas");
canvas.setAttribute('width', area.pos.w);
canvas.setAttribute('height', area.pos.h);
ctx = canvas.getContext("2d");
ctx.drawImage(img, area.pos.x, area.pos.y, area.pos.w, area.pos.h, 0, 0, area.pos.w, area.pos.h);
temp = document.createElement('a');
temp.setAttribute('download', 'area' + i + '.jpg');
temp.setAttribute('href', canvas.toDataURL("image/jpg").replace("image/jpg", "image/octet-stream"));
temp.click();
}
}
};
Jcrop.load('target').then(img => {
jcp = Jcrop.attach(img, {
multi: true
});
});
body {
margin: 0px;
padding: 0px;
background-color: #ededed;
}
.cropapp {
position: absolute;
width: 100%;
height: 100%;
left: 5%;
background: lightgray;
}
.cropbox {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
}
div {
position: relative;
overflow: hidden;
}
<head>
<title>Jcrop Example</title>
<link href="https://unpkg.com/jcrop#3.0.1/dist/jcrop.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://unpkg.com/jcrop#3.0.1/dist/jcrop.js"></script>
</head>
<body>
<div style="width:400px; margin:10px auto; padding:10px 0; overflow:hidden;"><a style="float:right; display:block; padding:10px 15px; color:#fff; background-color:#237dbd; font-size: 14px; font-weight:bold; border-radius:5px;" id="save">Save</a>
<input type="file" id="imageLoader" name="imageLoader" />
<!-- add this for file picker -->
</div>
<div>
<h1 style="font-family:Helvetica,sans-serif;">
Image Crop <span style="color:lightgray;"></span>
</h1>
<img id="target" style="background-size: cover !important;">
</div>
</body>
You can add any fixed width or height value to your <img id="target"/>. Just calculate and apply the change ratio to areas:
canvas = document.createElement("canvas");
displayWidth=img.width;
displayHeight=img.height;
realWidth = img.naturalWidth;
realHeight = img.naturalHeight;
widthRatio = Math.round(realWidth/displayWidth);
heightRatio = Math.round(realHeight/displayHeight);
width=area.pos.w*widthRatio
height=area.pos.h*heightRatio
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
ctx = canvas.getContext("2d");
ctx.drawImage(img, area.pos.x*widthRatio, area.pos.y*heightRatio, width, height, 0, 0, width, height);
and <img width="500" id="target"/> (or with css #target{width:500px})

Issue with IF statement attaching to canvas object - JS

l do believe to have an issue regarding an if statement that allows for the detection of where the canvas object, being the image that displays , should be located at within the canvas. There is a reproducible demo provided.
NOTE: I do want to KEEP the css alignment, canvasauto, and not remove it if possible.
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
var the_button = document.getElementById("the_button");
var the_background = document.getElementById("the_background");
var imageXpos = 500;
var imageYpos = 300;
var imageWidth = 110;
var imageHeight = 80;
var imagecheck_Xpos = canvas.width - imageXpos;
var imagecheck_Ypos = canvas.height - imageYpos;
window.onload = function() {
ctx.drawImage(the_background, 0, 0, canvas.width, canvas.height);
ctx.drawImage(the_button, imagecheck_Xpos, imagecheck_Ypos, imageWidth, imageHeight);
}
the_button_function = (event) => {
const {
x,
y
} = event;
/* l believe the issue is here */
if ((x >= imagecheck_Xpos && x < (imagecheck_Xpos + imageWidth)) &&
(y >= imagecheck_Ypos && y < (imagecheck_Ypos + imageHeight))) {
alert("<Button>")
}
}
canvas.addEventListener('click', (e) => the_button_function(e));
html,
body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
.canvasauto {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<html>
<canvas id="c" width="970" height="550" class="canvasauto"></canvas>
<img style="display: none;" id="the_button" src="https://i.imgur.com/wO7Wc2w.png" />
<img style="display: none;" id="the_background" src="https://img.freepik.com/free-photo/hand-painted-watercolor-background-with-sky-clouds-shape_24972-1095.jpg?size=626&ext=jpg" />
</html>
You're using x from the MouseEvent. This is an experimental feature which is an alias of clientX.
The clientX property gives the coordinates relative to the application's viewport.
You probably want offsetX, which is relative to (the padding edge of the) target node.
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
var the_button = document.getElementById("the_button");
var the_background = document.getElementById("the_background");
var imageXpos = 500;
var imageYpos = 300;
var imageWidth = 110;
var imageHeight = 80;
var imagecheck_Xpos = canvas.width - imageXpos;
var imagecheck_Ypos = canvas.height - imageYpos;
window.onload = function() {
ctx.drawImage(the_background, 0, 0, canvas.width, canvas.height);
ctx.drawImage(the_button, imagecheck_Xpos, imagecheck_Ypos, imageWidth, imageHeight);
}
the_button_function = (event) => {
const {
offsetX: x,
offsetY: y
} = event;
/* l believe the issue is here */
if ((x >= imagecheck_Xpos && x < (imagecheck_Xpos + imageWidth)) &&
(y >= imagecheck_Ypos && y < (imagecheck_Ypos + imageHeight))) {
alert("<Button>")
}
}
canvas.addEventListener('click', (e) => the_button_function(e));
html,
body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
.canvasauto {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<html>
<canvas id="c" width="970" height="550" class="canvasauto"></canvas>
<img style="display: none;" id="the_button" src="https://i.imgur.com/wO7Wc2w.png" />
<img style="display: none;" id="the_background" src="https://img.freepik.com/free-photo/hand-painted-watercolor-background-with-sky-clouds-shape_24972-1095.jpg?size=626&ext=jpg" />
</html>

How to Center Image above another image inside canvas element

I'm trying to add Images above another image in the canvas element
What I'm trying to reach:
Here is what I get:
images source: https://imgur.com/gallery/jaSdhQ9
and this my code:
var shirt = new Image();
shirt.src = "https://i.imgur.com/3rTZGXP.png";
var draw = new Image();
draw.src = "https://i.imgur.com/2abnbj1.png";
//draw.src = "https://i.imgur.com/TSJRGjo.png";
window.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(draw,0,0 );
ctx.drawImage(shirt,0,0,canvas.width,canvas.height);
}
#canvas{
width: 352px;
height: 448px;
left: 0px;
top: 0px;
user-select: none;
cursor: default;
}
<canvas id="canvas" width="352" height="448"></canvas>
you can set this like ctx.drawImage(draw,100,30,90,50 );
var shirt = new Image();
shirt.src = "https://i.imgur.com/3rTZGXP.png";
var draw = new Image();
draw.src = "https://i.imgur.com/2abnbj1.png";
//draw.src = "https://i.imgur.com/TSJRGjo.png";
window.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(draw,80,25,135,55 );
ctx.drawImage(shirt,0,0,canvas.width,canvas.height);
}
#canvas{
width: 352px;
height: 448px;
left: 0px;
top: 0px;
user-select: none;
cursor: default;
}
<canvas id="canvas"></canvas>
I think what you're looking for it's flex-box
#canvas{
display:flex;
justify-content: center;
align-content: center;
}
Here you can read more about the flex-box
thanks to #tokage-dizayn and https://stackoverflow.com/a/19473880/7511165
I came to this code
var shirt = new Image();
shirt.src = "https://i.imgur.com/3rTZGXP.png";
var draw = new Image();
draw.src = "https://i.imgur.com/2abnbj1.png";
//draw.src = "https://i.imgur.com/TSJRGjo.png";
window.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);
console.log(draw.height,draw.width);
var maxSize = 160;
var ratio = Math.min(maxSize / draw.width, maxSize / draw.height);
var width= draw.width*ratio,
height= draw.height*ratio;
console.log(ratio,width,height);
ctx.drawImage(draw,95,90,width,height);
ctx.drawImage(shirt,0,0,canvas.width,canvas.height);
}
#canvas{
width: 352px;
height: 448px;
left: 0px;
top: 0px;
user-select: none;
cursor: default;
}
<canvas id="canvas" width="352" height="448"></canvas>

HTML canvas painting start from right rather than top

I try to make drawing app on canvas. It works well in full screen but when I set its width and height according to a div container. It didn't work correctly. It starts painting from the right rather than the top left.
Here is my code pen link: https://codepen.io/pranaysharma995/pen/GRpGVmG.
div class="container">
<div class="col-md-4 col-md-offset-4 " id="canvas-container">
<canvas id="canvas">
</canvas></div>
#canvas-container{
position: relative;
width: 600px;
height: 600px;
background-color: #f3f6f7;
}
You have to offset the mouse position with the canvas.getBoundingClientRect
var canvas = document.querySelector("#canvas");
var container = document.querySelector(".col-md-4");
canvas.width = container.offsetWidth;
canvas.height = container.offsetHeight;
var c = canvas.getContext('2d');
c.fillStyle = "red";
c.strokeStyle = "red";
c.lineWidth = 5;
c.lineCap = "round";
function draw(e) {
var rect = canvas.getBoundingClientRect();
var x = e.clientX - rect.left
var y = e.clientY - rect.top
c.lineTo(x, y);
c.stroke();
}
canvas.addEventListener("mousemove", draw);
.container {
position: absolute;
left: 50px;
top: 25px
}
#canvas-container {
position: relative;
width: 200px;
height: 200px;
background-color: #f3f6f7;
}
<div class="container">
<div class="col-md-4 col-md-offset-4 " id="canvas-container">
<canvas id="canvas"></canvas>
</div>
</div>

How to center canvas javascript?

I'm having trouble trying to figure out how to center the canvas in the page. The canvas's code is in a javascript file. Here's the html for the page.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Game Tut</title>
</head>
<body>
<script src="game.js"></script>
</body>
</html>
I've tried something like this in the javascript file.
var canvas = document.createElement("canvas");
canvas.width = 800;
canvas.height = 600;
canvas.tabindex = 0;
canvas.style = "position: absolute; top: 50px; left: 50px; border:2px solid blue"
document.addEventListener('keydown', doKeyDown, true);
var ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
For some reason the blue border or the positioning don't seem to show up in Google Chrome.
var canvas = document.getElementsByName("canvas")[0];
var style = canvas.style;
style.marginLeft = "auto";
style.marginRight = "auto";
var parentStyle = canvas.parentElement.style;
parentStyle.textAlign = "center";
parentStyle.width = "100%";
OR
canvas.style = "position:absolute; left: 50%; width: 400px; margin-left: -200px;";
Try this:
var canvas = document.createElement("canvas");
canvas.width = 800;
canvas.height = 600;
canvas.tabindex = 0;
canvas.setAttribute('style', "position: absolute; left: 50%;margin-left:-400px; top: 50%;margin-top:-300px; border:2px solid blue");
var ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
document.addEventListener('keydown', doKeyDown, true);
Try this
the code below made the canvas center
canvas.style = "position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; margin: auto; border:2px solid blue";
By the way
document.addEventListener('keydown', doKeyDown, true);
I posted your code but chrome showed the 'doKeyDown' function is undefined so maybe you need to fix this error then your canvas will show correctly.
Error showed

Categories

Resources