GSAP SVG Transform Origin bad behavior - javascript

I'm trying to make this orbits rotates from the center (transfrom-origin: 50% 50%) but why it's still moving?
// Show Premium
function showPremuimPackage(el) {
// if Premium is selected
if(planetAdvance.hasClass('unselected') && planetEssential.hasClass('unselected')) {
let tlArrange = new TimelineMax();
tlArrange
// move selected
.to(premiumOrbit, .6,{transformOrigin:"center", rotation: '-50%', ease:Power1.easeOut})
.to(planetPremium, .6,{rotation: 50,transformOrigin:"50% 50%", scale: '2', ease:Power1.easeOut},'-=1')
// move unselected
// Advanced
.to(advanceOrbit, .6,{rotation: "50%", transformOrigin:"center", ease:Power1.easeOut},'-=.5')
// .to(planetAdvance, .6,{scale: '.4.5', rotation: -50,
transformOrigin:"50% 50%", ease:Power1.easeOut}, '-=.5')
// // Essential
// .to(essentialOrbit, .6,{rotation: '-50%',
transformOrigin:"50% 50%", ease:Power1.easeOut},'-=.5')
// .to(planetEssential, .6,{transformOrigin:"50% 50%",
ease:Power1.easeOut},'-=.5')
;
}
}
https://codepen.io/G-ROS/pen/WOrMrb
Why its doesn't rotate in the middle. And if you keep clicking the other planets it get a mess (need to uncomment to see it ). What I need to do to keep things always centered and not move around when rotating.
Thanks a lot guys.

Related

In vis.network, is it possible to set a node as the center of gravity?

I am using vis.js to show a network diagram.
I need to maintain the "edge length" after dragging a node so I use the physics option.
My problem is that the node I drag always bounce back to its original position, which I think is because of these options.
physics: {
forceAtlas2Based: {
gravitationalConstant: -150,
centralGravity: 0.005,
springLength: 180,
springConstant: 0.18,
},
maxVelocity: 146,
solver: "forceAtlas2Based",
timestep: 0.35,
stabilization: {
enabled: true,
iterations: 1000,
updateInterval: 25
}
}
So what I'm thinking right now is if there is a way, where I can change the "center" to be the node dragged so that the physics center of gravity revolves around it?
Or
Is there a way to restabilize the network without having the dragged node bounce back to its initial position?
There are currently no way to set selected node as center of gravity. But you could change their params like mass on some events like dragStart or dragEnd.
network.on("dragStart", function(e){
const id = network.getNode(e.nodes[0]).id
nodes.update([ { id, mass: 5 } ])
})
Also to you can run network.stopSimulation() or network.stabilize() to stop physics simulation.

how can i rotate a body with a tween?

i'm trying rotate a body that i had created, but it's appear rotated when refresh the page, i use matter js (http://brm.io/matter-js/) and tween js (https://www.createjs.com/tweenjs), matter js don't has tween, so for that reasson i used tween js, but donĀ“t works correctly
$(document).ready(function() {
//walls
barra_izquierda_vaso_moleculas = Bodies.rectangle(200,200,10,200,{isStatic: true});
barra_derecha_vaso_moleculas = Bodies.rectangle(500,200,10,200,{isStatic: true});
barra_inferior_vaso_moleculas = Bodies.rectangle(350,300,300,10,{isStatic: true});
//construction of my body
vaso_moleculas = Body.create({
parts: [barra_izquierda_vaso_moleculas, barra_derecha_vaso_moleculas, barra_inferior_vaso_moleculas],
restitution: 0,
friction: 0,
frictionStatic: 0,
frictionAir: 0,
inertia: Infinity,
mass: 1,
isStatic: true,
});
// add to world
World.add(world, vaso_moleculas);
//tween
createjs.Tween.get(vaso_moleculas).to(2, 9000);
});
You can draw your shape using p5.js and in your draw() loop, set your background with a lower alpha, such as background( 0, 32 ). This will create the illusion of motion blur or tweening. You can see it here, kind of.

How to import Javascript module

I'm trying to use http://spin.js.org/ to create a spinner on my site that starts spinning when an AJAX post fires and stops when it completes. I'm struggling to get the spinner working at all, though.
I have a node app and am templating with EJS. Under the usage section, spin.js's website says:
import {Spinner} from 'spin.js';
var opts = {
lines: 13, // The number of lines to draw
length: 38, // The length of each line
width: 17, // The line thickness
radius: 45, // The radius of the inner circle
scale: 1, // Scales overall size of the spinner
corners: 1, // Corner roundness (0..1)
color: '#ffffff', // CSS color or array of colors
fadeColor: 'transparent', // CSS color or array of colors
opacity: 0.25, // Opacity of the lines
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
fps: 20, // Frames per second when using setTimeout() as a fallback in IE 9
zIndex: 2e9, // The z-index (defaults to 2000000000)
className: 'spinner', // The CSS class to assign to the spinner
top: '50%', // Top position relative to parent
left: '50%', // Left position relative to parent
shadow: none, // Box-shadow for the lines
position: 'absolute' // Element positioning
};
var target = document.getElementById('foo');
var spinner = new Spinner(opts).spin(target);
I'm not sure where the import {Spinner} from 'spin.js' is supposed to go? I've searched around a lot and haven't been able to find out how to actually implement this. I found this example of a jquery plugin for spin.js but I'm struggling with that one as well. Any help would be much appreciated!
As of right now, this is what I have:
<div id="spinnerContainer" class="spinner" style="width:100px;height:100px;background-color: Gray; color:black;">
</div>
<script src="/scripts/spin.js/spin.js" type="text/javascript"></script>
<script>
var opts = {
lines: 20, // The number of lines to draw
length: 0, // The length of each line
width: 15, // The line thickness
radius: 42, // The radius of the inner circle
scale: 0.85, // Scales overall size of the spinner
corners: 1, // Corner roundness (0..1)
color: '#41d62b', // CSS color or array of colors
fadeColor: 'transparent', // CSS color or array of colors
opacity: 0.05, // Opacity of the lines
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
speed: 1, // Rounds per second
trail: 74, // Afterglow percentage
fps: 20, // Frames per second when using setTimeout() as a fallback in IE 9
zIndex: 2e9, // The z-index (defaults to 2000000000)
className: 'spinner', // The CSS class to assign to the spinner
top: '50%', // Top position relative to parent
left: '50%', // Left position relative to parent
shadow: 0, // Box-shadow for the lines
position: 'absolute' // Element positioning
};
var target = document.getElementById('spinnerContainer');
var spinner = new Spinner(opts).spin(target);
</script>
The script to load in spin.js is finding the file correctly, but then I get the error Uncaught SyntaxError: Unexpected token export referencing the line export { Spinner }; from spin.js
I also get an error saying Uncaught ReferenceError: Spinner is not defined which I assume is related to the error above but I'm not sure.
Perhaps all you want is to use a CDN version if you aren't set up to manage imports
var opts = {
lines: 20, // The number of lines to draw
length: 0, // The length of each line
width: 15, // The line thickness
radius: 42, // The radius of the inner circle
scale: 0.85, // Scales overall size of the spinner
corners: 1, // Corner roundness (0..1)
color: '#41d62b', // CSS color or array of colors
fadeColor: 'transparent', // CSS color or array of colors
opacity: 0.05, // Opacity of the lines
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
speed: 1, // Rounds per second
trail: 74, // Afterglow percentage
fps: 20, // Frames per second when using setTimeout() as a fallback in IE 9
zIndex: 2e9, // The z-index (defaults to 2000000000)
className: 'spinner', // The CSS class to assign to the spinner
top: '50%', // Top position relative to parent
left: '50%', // Left position relative to parent
shadow: 0, // Box-shadow for the lines
position: 'absolute' // Element positioning
};
var target = document.getElementById('spinnerContainer');
var spinner = new Spinner(opts).spin(target);
<script src="https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.3.2/spin.js"></script>
<div id="spinnerContainer" class="spinner" style="width:100px;height:100px;background-color: Gray; color:black;">
</div>
I was getting same error open the CDN file from #charlietfl answer copy it and replace it with your spin.js file. It will work.

Basic rotation using Snap.svg

I am trying to rotate the svg as it translates along, but i cant make it rotate on its axis. Is there a way to make it rotate on its own axis ?
jsFiddle
js Code
var paper = Snap('#svg');
matrix = new Snap.Matrix().translate(630, -95);
var start_gear = paper.path("M51.7,29.5v-7.2h-5.2c-0.5-3.1-1.6-6-3.3-8.5l3.4-3.4l-5.1-5.1l-3.2,3.2c-2.6-2-5.6-3.4-8.8-4V0.3h-7.1v4.1 c-3.4,0.5-6.5,1.7-9.2,3.6l-2.8-2.8l-5.1,5.1L8,12.9c-2,2.7-3.3,5.8-3.8,9.2H0v7.1h4.2c0.6,3.4,2,6.6,4,9.2l-2.8,2.8l5.1,5.1 l2.9-2.9c2.6,1.7,5.6,2.9,8.9,3.4v5.3h7.2v-5.4c3.1-0.6,6-1.9,8.5-3.7l3.6,3.6l5.1-5.1l-3.7-3.7c1.7-2.5,2.9-5.3,3.5-8.3H51.7z M25.8,38.1c-6.6,0-12-5.4-12-12c0-6.6,5.4-12,12-12c6.6,0,12,5.4,12,12C37.8,32.8,32.4,38.1,25.8,38.1z").transform(matrix);
start_gear.attr({fill: "#e8434d"});
start_gear.animate({ transform: 't630, 240 r540,22,20' }, 3000, mina.bounce);
If you don't include your own center of rotation, Snap will use its calculated bounding box center to rotate around. So it's as simple as removing that.
start_gear.animate({ transform: 't630, 240 r540' }, 3000, mina.bounce);
jsfiddle
If you only want to rotate, AND the element already has a transform applied to it, you need to include the previous transform, so it doesn't get overwritten. To get the previous transform, it's simply element.transform(). So the full command would be...
start_gear.animate({ transform: start_gear.transform() + 'r540' }, 3000, mina.bounce);
jsfiddle
See this demo, I used TimelineMax.js lib. it is Super easy and helpful in animation !
HTML code :
<svg id="svg" width="80vw" height="60vh" viewBox="0 0 1300 250">
<path class="myPath" d="M51.7,29.5v-7.2h-5.2c-0.5-3.1-1.6-6-3.3-8.5l3.4-3.4l-5.1-5.1l-3.2,3.2c-2.6-2-5.6-3.4-8.8-4V0.3h-7.1v4.1 c-3.4,0.5-6.5,1.7-9.2,3.6l-2.8-2.8l-5.1,5.1L8,12.9c-2,2.7-3.3,5.8-3.8,9.2H0v7.1h4.2c0.6,3.4,2,6.6,4,9.2l-2.8,2.8l5.1,5.1 l2.9-2.9c2.6,1.7,5.6,2.9,8.9,3.4v5.3h7.2v-5.4c3.1-0.6,6-1.9,8.5-3.7l3.6,3.6l5.1-5.1l-3.7-3.7c1.7-2.5,2.9-5.3,3.5-8.3H51.7z M25.8,38.1c-6.6,0-12-5.4-12-12c0-6.6,5.4-12,12-12c6.6,0,12,5.4,12,12C37.8,32.8,32.4,38.1,25.8,38.1z"></path>
JS code:
/*
var paper = Snap('#svg');
matrix = new Snap.Matrix().translate(630, -95);
var start_gear = paper.path("M51.7,29.5v-7.2h-5.2c-0.5-3.1-1.6-6-3.3-8.5l3.4-3.4l-5.1-5.1l-3.2,3.2c-2.6-2-5.6-3.4-8.8-4V0.3h-7.1v4.1 c-3.4,0.5-6.5,1.7-9.2,3.6l-2.8-2.8l-5.1,5.1L8,12.9c-2,2.7-3.3,5.8-3.8,9.2H0v7.1h4.2c0.6,3.4,2,6.6,4,9.2l-2.8,2.8l5.1,5.1 l2.9-2.9c2.6,1.7,5.6,2.9,8.9,3.4v5.3h7.2v-5.4c3.1-0.6,6-1.9,8.5-3.7l3.6,3.6l5.1-5.1l-3.7-3.7c1.7-2.5,2.9-5.3,3.5-8.3H51.7z M25.8,38.1c-6.6,0-12-5.4-12-12c0-6.6,5.4-12,12-12c6.6,0,12,5.4,12,12C37.8,32.8,32.4,38.1,25.8,38.1z").transform(matrix);
start_gear.attr({fill: "#e8434d"});
start_gear.animate({ transform: 't630, 240 r540,22,20' }, 3000, mina.bounce); */
var tl = new TimelineMax();
// Settings ...
tl.set(".myPath", {
x: 630,
y: -95,
fill: "#e8434d",
transformOrigin: "center"
})
// Animatiion ...
tl.to(".myPath", 3 , {
//x : "+= 500",
y: "+= 400",
rotation: "630",
ease: Bounce.easeOut
});

Make Slide in Animation JavaScript and HTML

How would one make a slide and fade in animation like in seen in the green and pink boxes on sharethis.com? In particular I like the one in the blue box with the arrows. Are there a set of JavaScript codes or CSS effects?
The easiest would probably be to use a library like this:
http://janpaepke.github.io/ScrollMagic/
I see that for the arrows effect the css-height property is animated when you scroll. This is done in javascript. But you can also achieve this effect through CSS3-transitions.
Update: Slide and wipe effects from the demo page:
// ani
var pinani = new TimelineMax()
// wipe
.add(TweenMax.to("#wipe", 1, {
width: "100%"
}))
// slide
.add(TweenMax.to("#slide", 1, {
top: "0%",
ease: Bounce.easeOut,
delay: 0.2
}));
// pin
new ScrollScene({
triggerElement: "section#pin",
duration: 1100
})
.on("progress", function () {
// keep centered even though width changes
$("#wipe h3").width($("#pin>h3").width());
})
.setTween(pinani)
.setPin("section#pin")
.addTo(controller);

Categories