I’m stuck.
I have a code that allows you to enter the base64 code into the input and get, after pressing the button, the full image and the second image as part of the full image (cropped), but how can I get the third image as another cropped part of the whole? So the third image will be also cropped image but another part, not similar as first cropped. I have tried many options and none of them work.
Also, I have an additional question: can I crop by percentage? Let’s say I have an image of 500 by 500 pixels where in any of its area a circle of 100 by 100 pixels is displayed. Let’s say such an image is cropped to a triangle because I set it that way in the code. But if I upload the same image but smaller, 100 by 100 pixels, then there will be no triangle. So I need the crop in percentage.
<!DOCTYPE html>
<html>
<head>
<title>example</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
$(document).ready(function () {
$("#Base64_to_image_button").click(function () {
document
.getElementById("image_output")
.setAttribute("src", $("#base64_input").val());
$("#image_output").show();
var canvasElement = document.createElement("canvas");
var canvasContext = canvasElement.getContext("2d");
var imageElement = document.querySelector("#image_output");
imageElement.onload = function () {
canvasContext.drawImage(
imageElement,
150, 180, 50, 50, 0, 0, 50, 50
);
var dataURL = canvasElement.toDataURL();
var croppedImg = document.querySelector("#cropped-img");
croppedImg.src = dataURL;
};
canvasElement.remove();
});
});
</script>
</head>
<body>
<input id=base64_input value=""/>
<button id=Base64_to_image_button>Go</button>
<hr>
<img id=image_output src="" alt="a">
<hr>
<img id=cropped-img src="" alt="b">
</body>
</html>
