Skip to content

Instantly share code, notes, and snippets.

@thebrainroom
Created February 14, 2017 09:22
Show Gist options
  • Save thebrainroom/2a24c7f59af626391c321e7913df831e to your computer and use it in GitHub Desktop.
Save thebrainroom/2a24c7f59af626391c321e7913df831e to your computer and use it in GitHub Desktop.
! function(e) {
"use strict";
e.ThreeSixty = function(t, r) {
var a, n = this,
i = [];
n.$el = e(t), n.el = t, n.$el.data("ThreeSixty", n), n.init = function() {
a = e.extend({}, e.ThreeSixty.defaultOptions, r), a.disableSpin && (a.currentFrame = 1, a.endFrame = 1), n.initProgress(), n.loadImages()
}, n.resize = function() {}, n.initProgress = function() {
n.$el.css({
width: a.width + "px",
height: a.height + "px",
"background-image": "none !important"
}), a.styles && n.$el.css(a.styles), n.responsive(), n.$el.find(a.progress).css({
marginTop: a.height / 2 - 15 + "px"
}), n.$el.find(a.progress).fadeIn("slow"), n.$el.find(a.imgList).hide()
}, n.loadImages = function() {
var t, r, o, s;
t = document.createElement("li"), s = a.zeroBased ? 0 : 1, r = a.imgArray ? a.imgArray[a.loadedImages] : a.domain + a.imagePath + a.filePrefix + n.zeroPad(a.loadedImages + s) + a.ext + (n.browser.isIE() ? "?" + (new Date).getTime() : ""), o = e("<img>").attr("src", r).addClass("previous-image").appendTo(t), i.push(o), n.$el.find(a.imgList).append(t), e(o).load(function() {
n.imageLoaded()
})
}, n.imageLoaded = function() {
a.loadedImages += 1, e(a.progress + " span").text(Math.floor(a.loadedImages / a.totalFrames * 100) + "%"), a.loadedImages >= a.totalFrames ? (a.disableSpin && i[0].removeClass("previous-image").addClass("current-image"), e(a.progress).fadeOut("slow", function() {
e(this).hide(), n.showImages(), n.showNavigation()
})) : n.loadImages()
}, n.showImages = function() {
n.$el.find(".txtC").fadeIn(), n.$el.find(a.imgList).fadeIn(), n.ready = !0, a.ready = !0, a.drag && n.initEvents(), n.refresh(), n.initPlugins(), a.onReady(), setTimeout(function() {
n.responsive()
}, 50)
}, n.initPlugins = function() {
e.each(a.plugins, function(t, r) {
if ("function" != typeof e[r]) throw new Error(r + " not available.");
e[r].call(n, n.$el, a)
})
}, n.showNavigation = function() {
if (a.navigation && !a.navigation_init) {
var t, r, i, o;
t = e("<div/>").attr("class", "nav_bar"), r = e("<a/>").attr({
href: "#",
"class": "nav_bar_next"
}).html("next"), i = e("<a/>").attr({
href: "#",
"class": "nav_bar_previous"
}).html("previous"), o = e("<a/>").attr({
href: "#",
"class": "nav_bar_play"
}).html("play"), t.append(i), t.append(o), t.append(r), n.$el.prepend(t), r.bind("mousedown touchstart", n.next), i.bind("mousedown touchstart", n.previous), o.bind("mousedown touchstart", n.play_stop), a.navigation_init = !0
}
}, n.play_stop = function(t) {
t.preventDefault(), a.autoplay ? (a.autoplay = !1, e(t.currentTarget).removeClass("nav_bar_stop").addClass("nav_bar_play"), clearInterval(a.play), a.play = null) : (a.autoplay = !0, a.play = setInterval(n.moveToNextFrame, a.playSpeed), e(t.currentTarget).removeClass("nav_bar_play").addClass("nav_bar_stop"))
}, n.next = function(e) {
e && e.preventDefault(), a.endFrame -= 5, n.refresh()
}, n.previous = function(e) {
e && e.preventDefault(), a.endFrame += 5, n.refresh()
}, n.play = function(e) {
var t = e || a.playSpeed;
a.autoplay || (a.autoplay = !0, a.play = setInterval(n.moveToNextFrame, t))
}, n.stop = function() {
a.autoplay && (a.autoplay = !1, clearInterval(a.play), a.play = null)
}, n.moveToNextFrame = function() {
1 === a.autoplayDirection ? a.endFrame -= 1 : a.endFrame += 1, n.refresh()
}, n.gotoAndPlay = function(e) {
if (a.disableWrap) a.endFrame = e, n.refresh();
else {
var t = Math.ceil(a.endFrame / a.totalFrames);
0 === t && (t = 1);
var r = t > 1 ? a.endFrame - (t - 1) * a.totalFrames : a.endFrame,
i = a.totalFrames - r,
o = 0;
o = e - r > 0 ? e - r < r + (a.totalFrames - e) ? a.endFrame + (e - r) : a.endFrame - (r + (a.totalFrames - e)) : i + e > r - e ? a.endFrame - (r - e) : a.endFrame + (i + e), r !== e && (a.endFrame = o, n.refresh())
}
}, n.initEvents = function() {
n.$el.bind("mousedown touchstart touchmove touchend mousemove click", function(e) {
e.preventDefault(), "mousedown" === e.type && 1 === e.which || "touchstart" === e.type ? (a.pointerStartPosX = n.getPointerEvent(e).pageX, a.dragging = !0) : "touchmove" === e.type ? n.trackPointer(e) : "touchend" === e.type && (a.dragging = !1)
}), e(document).bind("mouseup", function() {
a.dragging = !1, e(this).css("cursor", "none")
}), e(window).bind("resize", function() {
n.responsive()
}), e(document).bind("mousemove", function(e) {
a.dragging ? (e.preventDefault(), !n.browser.isIE && a.showCursor && n.$el.css("cursor", "url(assets/images/hand_closed.png), auto")) : !n.browser.isIE && a.showCursor && n.$el.css("cursor", "url(assets/images/hand_open.png), auto"), n.trackPointer(e)
}), e(window).resize(function() {
n.resize()
})
}, n.getPointerEvent = function(e) {
return e.originalEvent.targetTouches ? e.originalEvent.targetTouches[0] : e
}, n.trackPointer = function(e) {
a.ready && a.dragging && (a.pointerEndPosX = n.getPointerEvent(e).pageX, a.monitorStartTime < (new Date).getTime() - a.monitorInt && (a.pointerDistance = a.pointerEndPosX - a.pointerStartPosX, a.reverseDrag && (a.pointerDistance = a.pointerStartPosX - a.pointerEndPosX), a.endFrame = a.pointerDistance > 0 ? a.currentFrame + Math.ceil((a.totalFrames - 1) * a.speedMultiplier * (a.pointerDistance / n.$el.width())) : a.currentFrame + Math.floor((a.totalFrames - 1) * a.speedMultiplier * (a.pointerDistance / n.$el.width())), a.disableWrap && (a.endFrame = Math.min(a.totalFrames - (a.zeroBased ? 1 : 0), a.endFrame), a.endFrame = Math.max(a.zeroBased ? 0 : 1, a.endFrame)), n.refresh(), a.monitorStartTime = (new Date).getTime(), a.pointerStartPosX = n.getPointerEvent(e).pageX))
}, n.refresh = function() {
0 === a.ticker && (a.ticker = setInterval(n.render, Math.round(1e3 / a.framerate)))
}, n.render = function() {
var e;
a.currentFrame !== a.endFrame ? (e = a.endFrame < a.currentFrame ? Math.floor(.1 * (a.endFrame - a.currentFrame)) : Math.ceil(.1 * (a.endFrame - a.currentFrame)), n.hidePreviousFrame(), a.currentFrame += e, n.showCurrentFrame(), n.$el.trigger("frameIndexChanged", [n.getNormalizedCurrentFrame(), a.totalFrames])) : (window.clearInterval(a.ticker), a.ticker = 0)
}, n.hidePreviousFrame = function() {
i[n.getNormalizedCurrentFrame()].removeClass("current-image").addClass("previous-image")
}, n.showCurrentFrame = function() {
i[n.getNormalizedCurrentFrame()].removeClass("previous-image").addClass("current-image")
}, n.getNormalizedCurrentFrame = function() {
var e, t;
return a.disableWrap ? (e = Math.min(a.currentFrame, a.totalFrames - (a.zeroBased ? 1 : 0)), t = Math.min(a.endFrame, a.totalFrames - (a.zeroBased ? 1 : 0)), e = Math.max(e, a.zeroBased ? 0 : 1), t = Math.max(t, a.zeroBased ? 0 : 1), a.currentFrame = e, a.endFrame = t) : (e = Math.ceil(a.currentFrame % a.totalFrames), 0 > e && (e += a.totalFrames - (a.zeroBased ? 1 : 0))), e
}, n.getCurrentFrame = function() {
return a.currentFrame
}, n.responsive = function() {
a.responsive && a.limitMaxWidth && n.$el.css({
height: n.$el.find(".current-image").first().css("height"),
maxWidth: "100%"
}), a.responsive && !a.limitMaxWidth && n.$el.css({
height: n.$el.find(".current-image").first().css("height"),
width: "100%"
})
}, n.zeroPad = function(e) {
function t(e, t) {
var r = e.toString();
if (a.zeroPadding)
for (; r.length < t;) r = "0" + r;
return r
}
var r = Math.log(a.totalFrames) / Math.LN10,
n = 1e3,
i = Math.round(r * n) / n,
o = Math.floor(i) + 1;
return t(e, o)
}, n.browser = {}, n.browser.isIE = function() {
var e = -1;
if ("Microsoft Internet Explorer" === navigator.appName) {
var t = navigator.userAgent,
r = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");
null !== r.exec(t) && (e = parseFloat(RegExp.$1))
}
return -1 !== e
}, n.getConfig = function() {
return a
}, e.ThreeSixty.defaultOptions = {
dragging: !1,
ready: !1,
pointerStartPosX: 0,
pointerEndPosX: 0,
pointerDistance: 0,
monitorStartTime: 0,
monitorInt: 10,
ticker: 0,
speedMultiplier: 7,
totalFrames: 180,
currentFrame: 0,
endFrame: 0,
loadedImages: 0,
framerate: 60,
domains: null,
domain: "",
parallel: !1,
queueAmount: 8,
idle: 0,
filePrefix: "",
ext: "png",
height: 300,
width: 300,
styles: {},
navigation: !1,
autoplay: !1,
autoplayDirection: 1,
disableSpin: !1,
disableWrap: !1,
responsive: !1,
zeroPadding: !1,
zeroBased: !1,
plugins: [],
showCursor: !1,
drag: !0,
onReady: function() {},
imgList: ".threesixty_images",
imgArray: null,
playSpeed: 100,
limitMaxWidth: !0,
reverseDrag: !1
}, n.init()
}, e.fn.ThreeSixty = function(t) {
return Object.create(new e.ThreeSixty(this, t))
}
}(jQuery), "function" != typeof Object.create && (Object.create = function(e) {
"use strict";
function t() {}
return t.prototype = e, new t
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment