HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/hamsterjs/1.1.2/hamster.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-mousewheel/1.0.5/mousewheel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.4/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.4/utils/Draggable.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/ThrowPropsPlugin.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/3.0.11/pixi.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/lodash-mixins-01.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/LinkedList.js"></script>
<!-- APP -->
<main ng-app="app" ng-cloak>
<!-- <img src="https://fb-host-f12d4.firebaseapp.com/Lv1/img01.png" alt="" /> -->
<!-- <product-viewer ng-if="imagesLoaded"> -->
<product-viewer ng-if="imagesLoaded">
</product-viewer>
<script type="text/ng-template" id="product-viewer.html">
<!-- <div class="inner" msd-wheel="$ctrl.foo($event, $delta, $deltaX, $deltaY)"></div> -->
<section class="product-viewer-inner" msd-wheel="$ctrl.onMouseWheel($event, $delta, $deltaX, $deltaY)">
<image-rotator image="mario"
pan-x="$ctrl.panX"
pan-y="$ctrl.panY"
rotation="$ctrl.rotation"
zoom="$ctrl.zoom">
</image-rotator>
<pan-dragger drag-type="$ctrl.dragType"></pan-dragger>
<spin-dragger drag-type="$ctrl.dragType"></spin-dragger>
<!-- <drag-proxy></drag-proxy> -->
<section class="product-viewer-controls">
<button ng-click="$ctrl.setDragType('spin')">Spin</button>
<button ng-click="$ctrl.setDragType('pan')">Pan</button>
<button ng-click="$ctrl.resetImage()">Reset</button>
</section>
</section>
<!-- <image-rotator image="mario" rotation="$ctrl.rotation"></image-rotator>
<drag-proxy></drag-proxy> -->
</script>
</main>
html, body {
height: 100%;
box-sizing: border-box;
user-select: none;
}
main {
position: relative;
width: 100%;
height: 100%;
}
product-viewer {
display: block;
position: relative;
height: 100vh;
width: 100vw;
overflow: hidden;
}
pan-dragger,
spin-dragger {
display: block;
position: absolute;
top: 0;
left: 0;
width: 1px;
height: 1px;
border: 8px solid red;
// border-radius: 50%;
// transform: translate(-50%, -50%);
z-index: 10;
}
pan-dragger {
border: 8px solid blue;
}
spin-dragger {
border: 8px solid red;
}
image-rotator {
position: absolute;
top: 50%;
left: 50%;
display: block;
padding: 0;
margin: 0;
// transform: translate(-50%, -50%);
pointer-events: none;
border: 1px solid blue;
canvas {
display: block;
padding: 0;
margin: 0;
}
}
.product-viewer-inner {
position: absolute;
position: relative;
top: 0;
left: 0;
width: 100%;
height: 100%;
// border: 1px solid purple;
// background: purple;
}
.product-viewer-controls {
position: absolute;
padding: 15px;
background: #fafafa;
z-index: 20;
border: 1px solid #ccc;
}
namespace App {
console.clear();
const log = console.log.bind(log);
const baseURL = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/";
const loader = new PIXI.loaders.Loader(baseURL);
const spriteSheets = {
mario: {
prefix: "img",
type: ".png",
data: [
{ file: "mario2.json", size: 10 },
{ file: "mario3.json", size: 10 },
]
}
};
//
// TWEEN
// ===========================================================================
class Tween {
tween;
constructor(target, duration, from) {
_.assign(this, target);
TweenLite.from(target, duration, from);
}
}
//
// ANIMATOR
// ===========================================================================
class Animator {
animations = new LinkedList();
constructor(public target) {
}
kill() {
}
render() {
}
}
//
// IMAGE ROTATOR
// ===========================================================================
const ImageRotator = {
bindings: {
panX: "<",
panY: "<",
rotation: "<",
zoom: "<",
image: "@"
},
controller: class {
isDirty = true;
isReady = false;
isReset = false;
total = 0;
width = 0;
height = 0;
stage = new PIXI.Container();
sprite = null;
renderer = null;
constructor(
public $element,
public $scope,
public $attrs,
public $window,
public textures) {
$scope.$on("image:reset", () => {
// log("RESET")
// this.reset();
this.isReset = true;
});
// $window.addEventListener("resize", () => this.isDirty = true);
}
$onChanges(changes) {
// if (!this.isReady) return;
if (!this.isReady || this.isReset) return;
// if (!this.isReady || this.isReset) return;
if (changes.rotation) {
this.setRotation(changes.rotation.currentValue);
}
if (changes.zoom) {
this.setZoom(changes.zoom.currentValue);
}
if (changes.panX || changes.panY) {
let x = changes.panX ? changes.panX.currentValue : this.panX;
let y = changes.panY ? changes.panY.currentValue : this.panY;
this.setPosition(x, y);
}
}
$postLink() {
let textures = this.textures[this.image];
let texture = textures[0];
this.total = textures.length;
this.width = texture.width;
this.height = texture.height;
this.renderer = PIXI.autoDetectRenderer(this.width, this.height, {
backgroundColor: 0xFFFFFF,
});
this.sprite = new ProductSprite(textures);
this.stage.addChild(this.sprite);
this.$element.append(this.renderer.view);
// TweenLite.set(this.$element, { width: this.width, height: this.height });
TweenLite.set(this.$element, {
width: this.width,
height: this.height,
xPercent: -50,
yPercent: -50
});
TweenLite.ticker.addEventListener("tick", this.render, this);
this.isReady = true;
}
reset() {
this.setZoom(1);
this.setRotation(0, true);
this.setPosition(0, 0);
}
setPosition(x, y) {
TweenLite.to(this.$element, 0.1, {
// x: `+=${x}`,
// y: `+=${y}`,
x,
y,
});
}
setZoom(scale) {
TweenLite.to(this.$element, 0.1, { scale });
}
setRotation(rotation, animate) {
let total = this.total - 1;
let ratio = rotation / 360;
let index = Math.round(ratio * total);
index = index < 1 ? total + index : index - 1;
if (animate) {
TweenLite.to(this.sprite, 0.1, {
frame: total - index,
roundProps: "frame",
ease: Linear.easeNone
});
} else {
this.sprite.frame = total - index;
}
// this.sprite.frame = total - index;
// TweenLite.to(this.sprite, 0.1, {
// frame: total - index,
// roundProps: "frame",
// ease: Linear.easeNone
// });
}
resize() {
var vw = this.$window.innerWidth;
var vh = this.$window.innerHeight;
var scale = Math.min(Math.min(vw / this.width, vh / this.height), 1);
TweenLite.set(this.$element, { scale });
}
render() {
if (this.isDirty) {
this.resize();
this.isDirty = false;
}
if (this.isReset) {
this.setZoom(1);
this.setPosition(0, 0);
this.setRotation(0, true);
this.isReset = false;
}
this.renderer.render(this.stage);
}
}
}
//
// PRODUCT SPRITE
// ===========================================================================
class ProductSprite extends PIXI.Sprite {
_frame = 0;
total = this.frames.length;
constructor(public frames) {
super(frames[0]);
}
get frame() { return this._frame; }
set frame(n) {
this._frame = n % this.total;
this.texture = this.frames[this._frame];
}
}
//
// PAN DRAGGER
// ===========================================================================
const PanDragger = {
bindings: {
dragType: "<"
},
require: {
productViewer: "^productViewer"
},
controller: class {
moveX = 0;
moveY = 0;
lastX = 0;
lastY = 0;
isReady = false;
// rotation = 0;
transform = {};
draggable = null;
constructor(public $element, public $scope, public $attrs) {
$scope.$on("image:reset", () => {
this.lastX = 0;
this.lastY = 0;
this.moveX = 0;
this.moveY = 0;
// this.rotation = 0;
});
}
get x() { return this.transform.x; }
get y() { return this.transform.y; }
$onChanges(changes) {
if (!this.isReady) return;
if (changes.dragType) {
this.setActive(changes.dragType.currentValue);
}
}
$postLink() {
var parent = this.productViewer.$element;
this.draggable = new Draggable(this.$element, {
bounds: parent,
callbackScope: this,
// cursor: "ew-resize",
// maxDuration: 0.65,
// overshootTolerance: 0,
onPress: this.setPosition,
onDrag: this.updatePosition,
onThrowUpdate: this.updatePosition,
// onDragStart: this.updatePosition,
throwProps: true,
trigger: parent,
zIndexBoost: false
});
TweenLite.set(this.$element, { xPercent: -50, yPercent: -50 });
this.transform = this.$element[0]._gsTransform;
this.isReady = true;
this.setActive(this.dragType);
}
setActive(dragType) {
if (dragType === "pan") {
this.draggable.enable();
} else {
this.draggable.disable();
}
}
setPosition() {
TweenLite.killTweensOf(this.$element);
let x = this.lastX = this.draggable.pointerX;
let y = this.lastY = this.draggable.pointerY;
TweenLite.set(this.$element, { x, y });
this.draggable.update();
}
updatePosition() {
let x = this.x;
let y = this.y;
let dx = x - this.lastX;
let dy = y - this.lastY;
this.lastX = x;
this.lastY = y;
// this.rotation += dx;
this.moveX += dx;
this.moveY += dy;
// this.productViewer.updateRotation(this.rotation % 360);
this.productViewer.updatePosition(this.moveX, this.moveY);
}
}
};
//
// SPIN DRAGGER
// ===========================================================================
const SpinDragger = {
bindings: {
dragType: "<"
},
require: {
productViewer: "^productViewer"
},
controller: class {
// moveX = 0;
// moveY = 0;
lastX = 0;
lastY = 0;
isReady = false;
rotation = 0;
transform = {};
draggable = null;
constructor(public $element, public $scope, public $attrs) {
$scope.$on("image:reset", () => {
this.lastX = 0;
this.lastY = 0;
this.moveX = 0;
this.moveY = 0;
this.rotation = 0;
});
}
get x() { return this.transform.x; }
get y() { return this.transform.y; }
$onChanges(changes) {
if (!this.isReady) return;
if (changes.dragType) {
this.setActive(changes.dragType.currentValue);
}
}
$postLink() {
var parent = this.productViewer.$element;
this.draggable = new Draggable(this.$element, {
// bounds: parent,
callbackScope: this,
cursor: "ew-resize",
maxDuration: 0.65,
// overshootTolerance: 0,
onPress: this.setPosition,
onDrag: this.updateRotation,
onThrowUpdate: this.updateRotation,
// onDragStart: this.updatePosition,
throwProps: true,
trigger: parent,
zIndexBoost: false
});
TweenLite.set(this.$element, { xPercent: -50, yPercent: -50 });
this.transform = this.$element[0]._gsTransform;
this.isReady = true;
this.setActive(this.dragType);
}
setActive(dragType) {
if (dragType === "spin") {
this.draggable.enable();
} else {
this.draggable.disable();
}
}
setPosition() {
TweenLite.killTweensOf(this.$element);
let x = this.lastX = this.draggable.pointerX;
let y = this.lastY = this.draggable.pointerY;
TweenLite.set(this.$element, { x, y });
this.draggable.update();
}
updateRotation() {
let x = this.x;
let y = this.y;
let dx = x - this.lastX;
let dy = y - this.lastY;
this.lastX = x;
this.lastY = y;
this.rotation += dx;
// this.moveX += dx;
// this.moveY += dy;
this.productViewer.updateRotation(this.rotation % 360);
// this.productViewer.updatePosition(this.moveX, this.moveY);
}
}
};
//
// PRODUCT VIEWER
// ===========================================================================
const ProductViewer = {
templateUrl: "product-viewer.html",
bindings: {
// msdWheel: "&"
},
controller: class {
rotation = 0;
maxZoom = 3;
minZoom = 0.25;
zoom = 1;
zoomStep = 0.15;
zoomLevel = 1;
zoomDelta = 0;
zoomRatio = 0.15;
dragType = "spin";
offsetX = 0;
offsetY = 0;
panX = 0;
panY = 0;
constructor(public $element, public $scope, public $attrs) {
}
onMouseWheel($event, $delta, $deltaX, $deltaY) {
// log("DELTA", $delta)
// this.zoomDelta += $delta;
this.zoom = _.clamp(this.zoom + $delta * this.zoomStep, this.minZoom, this.maxZoom);
}
setDragType(type) {
this.dragType = type;
}
resetImage() {
this.$scope.$broadcast("image:reset");
this.rotation = 0;
this.zoom = 1;
}
updatePosition(x, y) {
if (this.dragType !== "pan") return;
this.$scope.$apply(() => {
this.offsetX = x;
this.offsetY = y;
this.panX = x;
this.panY = y;
});
}
updateRotation(rotation) {
if (this.dragType !== "spin") return;
this.$scope.$apply(() => this.rotation = rotation);
}
}
};
//
// LOAD ASSETS
// ===========================================================================
function loadAssets($rootScope, loader, spriteSheets, textures) {
angular.forEach(spriteSheets, spriteSheet => {
spriteSheet.data.forEach(data => loader.add(data.file));
});
loader.load(() => {
angular.forEach(spriteSheets, (spriteSheet, name) => {
let count = 1;
textures[name] = spriteSheet.data.reduce((frames, data) => {
for (let i = 0; i < data.size; i++) {
let index = i + count;
if (index < 10) index = "0" + index;
let frame = `${spriteSheet.prefix}${index}${spriteSheet.type}`;
frames.push(PIXI.Texture.fromFrame(frame));
}
count = data.size;
return frames;
}, []);
});
$rootScope.$apply(() => ($rootScope.imagesLoaded = true));
});
}
//
// SPRING EASE
// ===========================================================================
class SpringEase extends Ease {
constructor(public omega = 10, public zeta = 0.75) {
super();
}
getRatio(progress) {
var omega = this.omega;
var zeta = this.zeta;
var beta = Math.sqrt(1.0 - zeta * zeta);
progress = 1 - Math.cos(progress * Math.PI / 2);
progress = 1 / beta *
Math.exp(-zeta * omega * progress) *
Math.sin( beta * omega * progress + Math.atan(beta / zeta));
return 1 - progress;
}
}
//
// MODULE
// ===========================================================================
angular
// .module("app", [])
.module("app", ["monospaced.mousewheel"])
.run(loadAssets)
.constant("loader", loader)
.constant("spriteSheets", spriteSheets)
.value("textures", {})
.component("imageRotator", ImageRotator)
.component("productViewer", ProductViewer)
.component("spinDragger", SpinDragger)
.component("panDragger", PanDragger)
}
Also see: Tab Triggers