JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<div id="object">what the fuck</div>
<div id="lightsource"></div>
<script>
$(document).ready(function(){
var light = $("#lightsource");
var object = $("#object");
light.draggable({
drag: function() {
calculateShadow(light, object);
}
});
$( window ).resize(function() {
calculateShadow(light, object);
});
});
</script>
</body>
CSS
#object {
position: relative;
background-color: #333;
width: 250px;
height: 250px;
margin: 250px auto;
//box-shadow: 13px -13px 3px rgba(0, 0, 0, 0.5);
}
#lightsource {
position: absolute;
background-color: yellow;
border-radius: 50px;
width: 50px;
height: 50px;
left: 200px;
bottom: 200px;
}
JavaScript
function getCenter(element){
// http://stackoverflow.com/a/8027914/370539
var Offset = element.offset();
var width = element.width();
var height = element.height();
var CenterX = Offset.left + width / 2;
var CenterY = Offset.top + height / 2;
// create a vector
return { x : CenterX, y : CenterY };
}
function getDistance(vector1, vector2){
// http://snipplr.com/view/47207/
var xs = 0;
var ys = 0;
xs = vector2.x - vector1.x;
xs = xs * xs;
ys = vector2.y - vector1.y;
ys = ys * ys;
return Math.sqrt( xs + ys );
}
function getLength(v){
var aSq = Math.pow(v.x, 2);
var bSq = Math.pow(v.y, 2);
return Math.sqrt(aSq + bSq);
}
function calculateShadow(lightsource, object){
// first, get the center points
var v1 = getCenter(lightsource);
var v2 = getCenter(object);
// get the distance
var distance = -Math.round(getDistance(v1, v2) );
distance = distance + (distance/2); // add a little so that shadow is behind
// then find the center point of the shadow >>>>
// http://stackoverflow.com/a/12550458/370539
var v3 = { x: (v1.x - v2.x), y: (v1.y - v2.y) };
var length = getLength(v3);
var unitVector = { x: (v3.x/length), y: (v3.y/length) };
// unit vector * distance
var vd = {x: (unitVector.x * distance), y:(unitVector.y * distance)};
// vd + v1
var point = { x: (vd.x + v1.x), y: (vd.y + v1.y) };
// get difference between object center and corner
var diff = {x:0,y:0};
diff.x = v2.x - object.position().top;
diff.y = v2.y - object.position().left;
// apply this difference to the shadow center to get shadow corner
point.x = point.x - diff.x;
point.y = point.y - diff.y;
// finally, get point - object.position()
var shadowX = Math.round( point.x - object.position().top );
var shadowY = Math.round( point.y - object.position().left );
// calculate shadow blur
var blur =...