I have a string in javascript like :
"<div class="croppie-result" style="width: 584px; height: 584px;"><img src="screenshot.png" style="left: -391px; top: -92px;"></div>"
I want to extract or get values of left
, top
, width
from this string in variables x
,y
,w
.
x=391
y=92
w=584
How do I do this?
Note : Values of x and y are positive intentionally, not by mistake.
This is part of a task to crop the image or get a cropped image using croppie
.
Here is the complete code :
var el = document.getElementById('resizer-demo');
var resize = new Croppie(el, {
viewport: {
width: 300,
height: 300,
type: 'square'
},
boundary: {
width: 400,
height: 400
},
showZoomer: true,
enableResize: false,
enableOrientation: true,
mouseWheelZoom: 'ctrl'
});
resize.bind({
url: '/home/shashanksingh/Pictures/Screenshot.png',
});
//on button click
function myFunction() {
document.getElementById('msg').innerHTML = 'You clicked!';
resize.result('html').then(function(data) {
document.getElementById('msg').innerHTML = data.outerHTML;
debugger
// do something with cropped blob
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.min.css" rel="stylesheet" />
<center>
<div id='resizer-demo'></div>
<button type="button" id='crop_it' onclick='myFunction()'>Crop</button>
<div id='msg'>You can also use CTRL + mouse-wheel to zoom.</div>
<br><br><br>
</center>
Open to suggestions I have to make a server request. I was thinking of sending these coordinates as I wasn't able to do anything else.
This is a very basic way for beginner.You can easily get by
className
ofDiv
get the div element by className.
get
width
usingstyle.width
.get
'img'
childElement fromdiv
element.get
style.left
andstyle.top
from'img'
childElement.get
Integer
value usingparseInt()
andMath.abs()
for positive value.