Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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 class="head"><b>Canvas Carousel</b></div>
<div class="wrap">
	<canvas class="canvas imgInside"></canvas>
</div>
<div><button class="button" id="pre">pre</button><button class="button" id="next">next</button></div>

              
            
!

CSS

              
                html,
body {
	padding: 0;
	margin: 0;
}

.head {
	height: 80px;
	line-height: 80px;
	margin: 0;
	padding: 0 15px;
	text-align: left;
	color: #666;
	font-size: 18px;
	user-select: none;
	position: relative;
}

.head:before {
	content: '';
	width: 7px;
	height: 70%;
	background: #30983F;
	position: absolute;
	top: 15%;
	left: 0;
}

.head b {
	font-family: Georgia, serif;
	font-size: 18px;
	color: #666;
}

.button {
	width: 26%;
	height: 35px;
	font-size: 16px;
	background:#E55C5C;
	color:#fff;
	border-radius:4px;
	border:none;
	outline:none;
	-webkit-tap-highlight-color: #999;
}

.button:first-child {
	margin: 5px 8% 0 16%;
}

.button:last-child {
	margin: 5px 16% 0 8%;
}

.wrap {
	width: 100%;
}

.canvas {
	background: #f0f0f0;
	height: 200px;
	width: 100%;
}

@media screen and (max-width: 430px) {
	.head {
		height: 50px;
		line-height: 50px;
		padding: 10px 15px;
	}
	.head b {
		font-size: 14px;
		color: #666;
	}
}
              
            
!

JS

              
                /*******************************canvas carousel********************************/
function CanvasCarousel(canvas) {
	this.canvas = canvas;
	this.context = canvas.getContext('2d');
	this.width = canvas.width;
	this.height = canvas.height;
	this.touch_start;
	this.touch_last;
	this.touch_init = 0;
	this.imgs = [];
	this.isSmoothing;
	this.pos_end;
	this.time_start;
	this.time_continue;
	this.cur_index = 0;
	this.touch_start_time;
	var me = this;
	this.drawImgs = function(offsetX) {
		var len = this.imgs.length;
		var context = this.context;
		context.clearRect(0, 0, this.width * len, this.height);
		context.setTransform(1, 0, 0, 1, offsetX, 0);
		for (var i = 0; i < len; i++) {
			var image = this.imgs[i];
			if (image) {
				context.drawImage(image.img, 0, 0, image.img.width, image.img.height, this.width * i + image.center_x, image.center_y, image.img_w, image.img_h);
			}
		}
	};
	this.addImages = function(images) {
		var len = images.length;
		var context = this.context;
		if (!len) {
			console.log("images array is empty!");
			return;
		}
		for (var i = 0; i < len; i++) {
			// IIFE
			var img = new Image();
			var url = images[i] + "-middle";
			(function(img, index) {
				img.onload = function() {
					var image = {};
					var img_w = img.width * pixelRatio > me.width ? me.width : img.width * pixelRatio;
					var img_h = img.height * pixelRatio > me.height ? me.height : img.height * pixelRatio;
					var scale = (img_w / img.width < img_h / img.height) ? (img_w / img.width) : (img_h / img.height);
					img_w = img.width * scale;
					img_h = img.height * scale;
					image.img = img;
					image.img_w = img_w;
					image.img_h = img_h;
					image.center_x = (me.width - img_w) / 2;
					image.center_y = (me.height - img_h) / 2;
					me.imgs[index] = image;
					if (index === len - 1) {
						me.drawImgs();
					}
				};
			})(img, i);
			img.src = url;
		}
	};
	this.smoothScroll = function() {
		var now = +new Date();
		if (now < me.time_start + me.time_continue) {
			if (me.pos_end - me.touch_init === 0) {
				return;
			}
			me.isSmoothing = true;
			me.touch_init = me.touch_init + (me.pos_end - me.touch_init) * ((now - me.time_start) / me.time_continue);
			me.drawImgs(me.touch_init);
			window.requestAnimationFrame(me.smoothScroll);
		} else {
			me.drawImgs(me.pos_end);
			me.touch_init = me.pos_end;
			me.isSmoothing = false;
		}
	};
	this.switchImg = function(index) {
		index = index < 0 ? 0 : (index >= this.imgs.length ? (this.imgs.length - 1) : index);
		this.cur_index = index;
		this.pos_end = -index * this.width;
		this.time_continue = 500;
		this.time_start = +new Date();
		this.smoothScroll();
	};
	this.canvas.addEventListener("touchstart", function(e) {
		me.touch_start = e.changedTouches[0].clientX;
		me.touch_last = me.touch_init;
		me.touch_start_time = +new Date();
	});
	this.canvas.addEventListener("touchmove", function(e) {
		var move = e.changedTouches[0].clientX - me.touch_start;
		var offsetX = me.touch_last + move * pixelRatio;
		if (offsetX < -me.width * (me.imgs.length - 1)) {
			offsetX = -me.width * (me.imgs.length - 1);
		}
		if (offsetX > 0) {
			offsetX = 0;
		}
		me.touch_init = offsetX;
		me.drawImgs(offsetX);
	});
	this.canvas.addEventListener("touchend", function(e) {
		var speed = (me.touch_init - me.touch_start) / (+new Date() - me.touch_start_time);
		var last_index = me.cur_index;
		var pos_last = -last_index * me.width;
		var move = me.touch_init - pos_last;
		var index = me.cur_index;
		if (move < -0.2 * me.width) {
			index = last_index + 1;
		} else if (move > 0.2 * me.width) {
			index = last_index - 1;
		} else if (Math.abs(speed) > 0.8) {
			if (move < 0) {
				index = last_index + 1;
			} else if (move > 0) {
				index = last_index - 1;
			}
		}
		me.switchImg(index);
	});
}
/**************************************************************************/
var canvas_carousel = document.querySelector(".canvas.imgInside");
var pixelRatio = window.devicePixelRatio || 1;
canvas_carousel.width = window.innerWidth * pixelRatio;
canvas_carousel.height = 200 * pixelRatio;

var images = ["http://oenqoxl4h.bkt.clouddn.com/list07.jpeg", "http://oenqoxl4h.bkt.clouddn.com/list06.jpeg", "http://oenqoxl4h.bkt.clouddn.com/list05.JPG", "http://oenqoxl4h.bkt.clouddn.com/list01.jpg", "http://oenqoxl4h.bkt.clouddn.com/list04.JPG"];
var canvasCarousel = new CanvasCarousel(canvas_carousel);
canvasCarousel.addImages(images);

document.querySelector("#pre").addEventListener("click", function() {
	canvasCarousel.switchImg(canvasCarousel.cur_index - 1);
});
document.querySelector("#next").addEventListener("click", function() {
	canvasCarousel.switchImg(canvasCarousel.cur_index + 1);
});
              
            
!
999px

Console