Skip to content

Instantly share code, notes, and snippets.

@badu-ser
Created January 4, 2025 20:12
Show Gist options
  • Save badu-ser/39333e9892958007606053f1812cfb9b to your computer and use it in GitHub Desktop.
Save badu-ser/39333e9892958007606053f1812cfb9b to your computer and use it in GitHub Desktop.
Heart Button
<html>
<body>
<div id='heart' class='button'></div>
</body>
</html>
$( document ).ready(function() {
var scaleCurve = mojs.easing.path('M0,100 L25,99.9999983 C26.2328835,75.0708847 19.7847843,0 100,0');
var el = document.querySelector('.button'),
// mo.js timeline obj
timeline = new mojs.Timeline(),
// tweens for the animation:
// burst animation
tween1 = new mojs.Burst({
parent: el,
radius: { 0: 100 },
angle: { 0: 45 },
y: -10,
count: 10,
radius: 100,
children: {
shape: 'circle',
radius: 30,
fill: [ 'red', 'white' ],
strokeWidth: 15,
duration: 500,
}
});
tween2 = new mojs.Tween({
duration : 900,
onUpdate: function(progress) {
var scaleProgress = scaleCurve(progress);
el.style.WebkitTransform = el.style.transform = 'scale3d(' + scaleProgress + ',' + scaleProgress + ',1)';
}
});
tween3 = new mojs.Burst({
parent: el,
radius: { 0: 100 },
angle: { 0: -45 },
y: -10,
count: 10,
radius: 125,
children: {
shape: 'circle',
radius: 30,
fill: [ 'white', 'red' ],
strokeWidth: 15,
duration: 400,
}
});
// add tweens to timeline:
timeline.add(tween1, tween2, tween3);
// when clicking the button start the timeline/animation:
$( ".button" ).click(function() {
if ($(this).hasClass('active')){
$(this).removeClass('active');
}else{
timeline.play();
$(this).addClass('active');
}
});
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdn.jsdelivr.net/mojs/latest/mo.min.js"></script>
html{
padding:0;
margin: 0;
min-height:100%;
}
body{
background: #111;
position:relative;
min-height:100%;
}
.button{
width: 50px;
height: 50px;
top:50%;
position: fixed;
left: 50%;
margin-top: -45px;
margin-left: -50px;
border-radius: 5px;
background: none;
cursor: pointer;
transition: background 0.5s ease;
}
.button:hover{
}
.active#heart:before,.active#heart:after{
background: red !important;
}
#heart {
width: 100px;
height: 90px;
transition: background 0.5s ease;
}
#heart:before,
#heart:after {
transition: background 0.5s ease;
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: dimgrey;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
#heart:after {
left: 0;
transform: rotate(45deg);
transform-origin :100% 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment