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

              
                <p class="message">Click anywhere</p>
<div id="canvas">
	<div v-for="person in persons">
		<person-component :person="person"></person-component>
	</div>
</div>
              
            
!

CSS

              
                p, div {
	margin: 0;
	padding: 0;
	font-family: 'Nunito', sans-serif;
}
.message {
	text-align: center;
	padding: 10px;
	font-size: 24px;
	position: absolute;
	width: 100%;
	box-sizing: border-box;
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
	color: #ddd;
}
#canvas {
	// background-color: #eee;
	width: 100%;
	height: 100%;
	position: absolute;
	overflow: hidden;
}
.person {
	position: absolute;
	width: 50px;
	height: 80px;
	transform: translate(-50%, -50%);
	border-radius: 5px;
	color: rgba(#4b4e53, 0.6);
	letter-spacing: 0.05em;
	&:hover {
		background-color: rgba(0, 0, 0, 0.05);
		color: #4b4e53;
		cursor: pointer;
	}
	&__body {
		background: url(https://www.graphicartsunit.com/codepen/images/persons.gif) no-repeat left top;
		position: absolute;
		width: 50px;
		height: 80px;
		left: 0;
		top: 0;
	}
	&__name {
		position: absolute;
		top: 80px;
		left: 50%;
		transform: translateX(-50%);
		font-size: 11px;
		line-height: 1.5;
		text-align: center;
	}
}
              
            
!

JS

              
                // 指定範囲のランダム整数を取得する
const getRandomRange = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

// 名前用の配列
const NAMES = ['Kyoshuke', 'Tomoyasu', 'Tsunematu', 'Makoto'];
const FASHION_NUM = 5;
const POSE_NUM = 4;

// Classの定義
class Person {
	constructor(x=0, y=0) {
		this.locate = [x, y];
		this.name = NAMES[getRandomRange(0, NAMES.length - 1)];
		this.fashion = getRandomRange(0, FASHION_NUM - 1);
		this.pose = getRandomRange(0, POSE_NUM - 1);
	}
	set locate(point) {
		this._locate = point;
	}
	set name(name) {
		this._name = name;
	}
	set fashion(index) {
		this._fashion = index;
	}
	set pose(index) {
		this._pose = index;
	}
	get locate() {
		return this._locate;
	}
	get name() {
		return this._name;
	}
	get fashion() {
		return this._fashion;
	}
	get pose() {
		return this._pose;
	}
	changePose() {
		let newIndex = getRandomRange(0, POSE_NUM - 1);
		while (newIndex === this._pose) {
			newIndex = getRandomRange(0, POSE_NUM - 1);
		}
		this._pose = newIndex;
	}
}

// Vueインスタンスを作成
let v_person = new Vue({
	el: '#canvas',
	data: {
		persons: []
	},
	components: {
		'person-component': {
			template: `
				<div class="person" @click="onclick" :style="vc_pos_style">
					<div class="person__body" :class="vc_class" :style="vc_bg_style"></div>
					<p class="person__name">{{ person.name }}</p>
				</div>
			`,
			props: ['person'],
			methods: {
				onclick: function (event) {
					event.stopPropagation();
					this.person.changePose(getRandomRange(0, POSE_NUM - 1));
				}
			},
			computed: {
				// 座標算出
				vc_pos_style: function () {
					return {
						left: this.person.locate[0] + 'px',
						top: this.person.locate[1] + 'px'
					};
				},
				// 背景画像位置算出
				vc_bg_style: function () {
					return {
						backgroundPositionY: (this.person.fashion * -80) + 'px',
						backgroundPositionX: (this.person.pose * -50) + 'px'
					};
				},
				// ファッションとポーズclass算出
				vc_class: function () {
					return 'person--fashion-' + this.person.fashion + ' person--pose-' + this.person.pose;
				}
			}
		}
	}
});

window.addEventListener('load', () => {

	// クリックで人を追加
	let canvas = document.querySelector('#canvas');
	canvas.addEventListener('click', event => {
		event.stopPropagation();
		v_person.persons.push(new Person(event.offsetX, event.offsetY));
	});

	// タイマーで生成する場合
	// let size = [canvas.clientWidth, canvas.clientHeight];
	// window.addEventListener('resize', event => {
	// 	size = [canvas.clientWidth, canvas.clientHeight];
	// });
	// let timer = setInterval( () => {
	// 	v_person.persons.push(new Person(getRandomRange(0, size[0]), getRandomRange(0, size[1])));
	// }, 200);
	// canvas.addEventListener('click', (event) => {
	// 	clearInterval(timer);
	// });

});
              
            
!
999px

Console