Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container">
  <img src="https://drive.google.com/uc?export=view&id=1u2w3RmLM8pn9aGIBBogtAPoroumip7a9" id="bird" class="resting">
</div>
              
            
!

CSS

              
                * {
  -webkit-user-select: none;
  user-select: none;
}
#container {
  position: relative;
  width: 600px;
  height: 400px;
  background-color: #fbf9eb;
}
#bird {
  position: absolute;
  left: 50px;
  top: 50px;
  width: 80px;
  transform: scale(-1,1);
}
#bird.resting {
  opacity: 0.4;
}
#bird:hover{
  cursor: pointer;
  opacity: 1;
}
              
            
!

JS

              
                window.onload = function() {
	let resting = true;
	const elem = document.getElementById("bird");
	elem.addEventListener('click', () => {
		if (resting) {
			resting = !resting;
			elem.classList.remove("resting");
			sequenceTasks(TASKS, msec)
			.then(() => {
				resting = !resting;
				elem.classList.add("resting");
				elem.style.opacity = "";
				elem.style.transform = `scale(-1,1)`;	// Left and right flip
			});
		}
	});
	const TASKS = [{
		fn: animateTranslate,
		arg: {
			elem: elem,
			time: 500,		// animation time
			sx: elem.offsetLeft,
			sy: elem.offsetTop,
			ex: 450,
			ey: 150,
			delay: 400		// start delay
		}
	}, {
		fn: animateZoom,
		arg: {
			elem: elem,
			time: 1000,
			rate: 2,
			delay: 0
		}
	}, {
		fn: animateZoom,
		arg: {
			elem: elem,
			time: 200,
			rate: 0.5,
			delay: 0
		}
	}, {
		fn: animateRotate,
		arg: {
			elem: elem,
			time: 1000,
			deg: 360*2,
			delay: 0
		}
	}, {
		fn: animateTranslate,
		arg: {
			elem: elem,
			time: 800,
			sx: 450,
			sy: 150,
			ex: 100,
			ey: 250,
			delay: 0
		}
	}, {
		fn: animateTranslate,
		arg: {
			elem: elem,
			time: 100,
			sx: 100,
			sy: 250,
			ex: elem.offsetLeft,
			ey: elem.offsetTop,
			delay: 0
		}
	}, {
		fn: animateFade,
		arg: {
			elem: elem,
			time: 1000,
			opacity: 0.4,
			delay: 0
		}
	}];
	const msec = 400;			// promise wait

	const wait = msec => new Promise(resolve => setTimeout(resolve, msec));

	function animateTranslate(arg) {
		return new Promise(resolve => {
			const diffX = arg.ex - arg.sx;
			const diffY = arg.ey - arg.sy;
			let progress = 0;

			requestAnimationFrame(startTime => {
				update(startTime);
				
				function update(timeStamp) {
					progress = (timeStamp - startTime - arg.delay) / arg.time;
					if (progress > 1) progress = 1;
					if (progress > 0) {	// progress is a negative value during the delay
						const resultX = arg.sx + diffX * progress;
						const resultY = arg.sy + diffY * progress;
						arg.elem.style.left = `${resultX}px`;
						arg.elem.style.top = `${resultY}px`;
					}
					if (progress < 1) {
						requestAnimationFrame(update);
					} else {
						console.log("animate translate resolve");
						resolve();
					}
				}
			});
		});
	}

	function animateZoom(arg) {
		return new Promise(resolve => {
			const width = arg.elem.offsetWidth;
			const height = arg.elem.offsetHeight;
			const cx = arg.elem.offsetLeft + width / 2;
			const cy = arg.elem.offsetTop + height / 2;
			const diffW = width * arg.rate - width;
			const diffH = height * arg.rate - height;
			let progress = 0;

			requestAnimationFrame(startTime => {
				update(startTime);

				function update(timeStamp) {
					progress = (timeStamp - startTime - arg.delay) / arg.time;
					if (progress > 1) progress = 1;
					if (progress > 0) {
						const resultW = width + diffW * progress;
						const resultH = height + diffH * progress;
						arg.elem.style.width = `${resultW}px`;
						arg.elem.style.height = `${resultH}px`;
						arg.elem.style.left = `${cx - resultW/2}px`;
						arg.elem.style.top = `${cy - resultH/2}px`;
					}
					if (progress < 1) {
						requestAnimationFrame(update);
					} else {
						console.log("animate zoom resolve");
						resolve();
					}
				}
			});
		});
	}

	function animateRotate(arg) {
		return new Promise(resolve => {
			let progress = 0;

			requestAnimationFrame(startTime => {
				update(startTime);

				function update(timeStamp) {
					progress = (timeStamp - startTime - arg.delay) / arg.time;
					if (progress > 1) progress = 1;
					if (progress > 0) {
						const resultR = arg.deg * progress;
						arg.elem.style.transform = `rotate(${resultR}deg)`;
					}
					if (progress < 1) {
						requestAnimationFrame(update);
					} else {
						arg.elem.style.transform = `scale(1,1)`;	// Left and right flip
						console.log(`animate rotate resolve`);
						resolve();
					}
				}
			});
		});
	}

	function animateFade(arg) {
		return new Promise(resolve => {
			let progress = 0;
			const diff = 1 - arg.opacity;

			requestAnimationFrame(startTime => {
				update(startTime);

				function update(timeStamp) {
					progress = (timeStamp - startTime - arg.delay) / arg.time;
					if (progress > 1) progress = 1;
					if (progress > 0) {
						const resultO = 1 - (diff * progress);
						arg.elem.style.opacity = `${resultO}`;
					}
					if (progress < 1) {
						requestAnimationFrame(update);
					} else {
						console.log("animate fade resolve");
						resolve();
					}
				}
			});
		});
	}

	function sequenceTasks(tasks, msec) {
		return tasks.reduce((promise, task) => {
			return promise.then(() => {
				return task.fn(task.arg);
			}).then(() => {
				console.log("wait");
				return wait(msec);
			});
		}, Promise.resolve());  // initialValue
	}

};	// window.onload
              
            
!
999px

Console