123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <main>
    <div class="wrapper">
        <section>
            <div>
                <h2>WebGL</h2>
                <p>縦にドラッグするとカメラが動きます</p>
                <div class="canvas-frame-wrapper">
                    <div id="canvas-frame"></div>
                </div>
            </div>
        </section>
    </div>
</main>

<script src="https://sterfield.co.jp/demo/ninomiya/108/js/jquery-2.2.4.min.js"></script>
<script src="https://sterfield.co.jp/demo/ninomiya/108/js/three.min.js"></script>
            
          
!
            
              html, body {
    font-family: "游ゴシック体", "Yu Gothic", YuGothic, "ヒラギノ角ゴ Pro","Hiragino Kaku Gothic Pro","メイリオ",Meiryo,Osaka,"MS Pゴシック","MS PGothic",sans-serif;
    font-size: 14px;
}
*{
    padding: 0;
    margin: 0;
}
*,
*::before,
*::after {
    box-sizing: border-box;
}
body{
    background: #f4f3f3;
    color: #2c2c2c;
    letter-spacing: 0.025em;
    line-height: 1.5;
}
a {
    color: #2c2c2c;
    text-decoration: none;
}
ul li,
ol li {
    list-style: none;
}
header {
    padding: 30px;
    text-align: center;
}
h1 {
    font-size: 1.33rem;
}
nav {
    margin-bottom: 30px;
}
nav ul {
    text-align: center;
}
ul li {
    display: inline-block;
    margin: 0 15px;
    font-weight: bold;
}
ul li a {
    display: inline-block;
}
ul li a.active {
    border-bottom: solid 2px #5e9bff;
    color: #5e9bff;
}
main {
    display: block;
    max-width: 480px;
    height: 400px;
    margin: 30px auto 60px;
    text-align: center;
    position: relative;
    -moz-perspective: 2000;
    -webkit-perspective: 2000;
    -ms-perspective: 2000;
    perspective: 2000;
}
.wrapper {
    height: 100%;
    margin-bottom: 15px;
    position: relative;
    -webkit-transform: rotateY(0deg);
    -ms-transform: rotateY(0deg);
    transform: rotateY(0deg);
}
.wrapper.animate {
    -webkit-transition: 0.5s linear;
    -ms-transition: 0.5s linear;
    transition: 0.5s linear;
    -webkit-transform: rotateY(90deg);
    -ms-transform: rotateY(90deg);
    transform: rotateY(90deg);
}
.wrapper.animateEnd {
    -webkit-transition: 0.5s linear;
    -ms-transition: 0.5s linear;
    transition: 0.5s linear;
    -webkit-transform: rotateY(180deg);
    -ms-transform: rotateY(180deg);
    transform: rotateY(180deg);
}
section {
    display: table;
    width: 100%;
    height: 100%;
    border-radius: 8px;
    background: #000;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1;
}
section div {
    display: table-cell;
    color: #fff;
    text-align: center;
    vertical-align: middle;
}
section h2 {
    margin-bottom: 20px;
}
section figure canvas {
    width: 150px;
    height: 150px;
    border-radius: 12px;
}
section p {
    max-width: 320px;
    padding: 0 15px;
    margin: 5px auto;
}
.canvas-frame-wrapper {
    display: block;
    width: 100%;
    padding-bottom: 50%;
    position: relative;
}
#canvas-frame {
    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
}
#canvas-frame canvas {
  width: 100%;
  height: 100%;
}



footer {
    text-align: center;
}



@media only screen and (max-width: 959px) {

html, body {
    font-size: 10px;
}
main {
    margin-top: 0;
    padding: 30px 15px;
}
ul li {
    margin: 0 5px;
}

}
            
          
!
            
              "use strict";

(function () {

window.addEventListener("load", function() {
    threeStart();
});

function threeStart() {
    initThree(); //Three.jsを初期化
    initCamera(); //カメラを初期化
    initObject(); //オブジェクトを初期化
    draw(); //描画実行
}

//各共通オブジェクトを格納する変数を初期化
var renderer, //レンダラーオブジェクト
    scene, //シーンオブジェクト
    camera, //カメラオブジェクト
    canvasFrame; //canvasの枠となるDOM要素

/* Three.jsの初期設定 */
function initThree() {
    canvasFrame = document.getElementById("canvas-frame"); //WebGLのCanvasを設置する親要素を取得
    //レンダラーを設定
    renderer = new THREE.WebGLRenderer({
        antialias: true //アンチエイリアスをONに
    });

    renderer.setSize(canvasFrame.clientWidth, canvasFrame.clientHeight); //親要素の大きさからレンダラーのサイズを設定
    canvasFrame.appendChild(renderer.domElement); //Canvasを親要素に設置

    renderer.setClearColor(0x000000, 0); //背景色を設定

    scene = new THREE.Scene(); //シーンを設定
}

/* カメラを設定 */
function initCamera() {
    camera = new THREE.PerspectiveCamera(45, canvasFrame.clientWidth / canvasFrame.clientHeight, 1, 10000);
    /* パースのついたカメラを設定。視野角, アスペクト比, カメラから視体積の手前までの距離、カメラから視体積の奥までの距離を指定 */
    camera.position.set(80, 80, 80); //カメラの位置をx, y ,zで指定
    camera.up.set(0, 0, 1); //カメラの上の方向
    camera.lookAt({ x: 0, y: 0, z: 0 }); //カメラの視野中心座標
}

var axis, //軸オブジェクトの初期化
    box, //立方体オブジェクトの初期化
    light; //ライトオブジェクトの初期化
function initObject() {

    //軸
    axis = new THREE.AxisHelper(50); //軸オブジェクトの呼び出し
    scene.add(axis); //シーンに追加
    axis.position.set(0, 0, 0); //位置を設定

    //立方体
    var geometry = new THREE.BoxGeometry( 30, 30, 30 ), //BOXオブジェクトの呼び出し
        material = new THREE.MeshPhongMaterial( { //質感の設定
            color: 0x156289, //色
            emissive: 0x072534, //発光色
            shading: THREE.FlatShading //フラットシェーディングを適用
        }),
        cube = new THREE.Mesh( geometry, material ); //Meshを設定
    scene.add( cube ); //シーンに追加
    cube.position.set(15, 15, 15); //位置を設定

    //ライト
    var light = new THREE.PointLight( 0xffffff, 1, 0 ); //点ライトオブジェクトの呼び出し
    scene.add( light ); //シーンに追加
    light.position.set( 0, 100, 200 ); //位置を設定
}

function draw() {
    renderer.clear(); //レンダラーの描画をクリア
    renderer.render(scene, camera); //レンダリング
}


/*-- 操作 --*/
//イベントの振り分け
var EVENT = {};
if ('ontouchstart' in window) {
  EVENT.TOUCH_START = 'touchstart';
  EVENT.TOUCH_MOVE = 'touchmove';
  EVENT.TOUCH_END = 'touchend';
} else {
  EVENT.TOUCH_START = 'mousedown';
  EVENT.TOUCH_MOVE = 'mousemove';
  EVENT.TOUCH_END = 'mouseup';
}

$(function() {
    var $contentFlickData = {};
    $("#canvas-frame").on(EVENT.TOUCH_START, function(e) { //ドラッグ開始
        $contentFlickData = {
          "pageY": e.originalEvent.pageY ? e.originalEvent.pageY : e.originalEvent.changedTouches[0].pageY
        }
    }).on(EVENT.TOUCH_MOVE, function(e) { //ドラッグ中
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
        var pageY = e.originalEvent.pageY ? e.originalEvent.pageY : e.originalEvent.changedTouches[0].pageY;
        //camera.position.set(80, 80 +  , 80);
        //draw();
        if($contentFlickData.pageY) {
            camera.position.set(80 + Math.floor(($contentFlickData.pageY - pageY) / 2), 80 + Math.floor(($contentFlickData.pageY - pageY) / 2) , 80); //カメラをドラッグに合わせて移動
            draw();
        }
    }).on(EVENT.TOUCH_END, function(e) {
        var pageY = e.originalEvent.pageY ? e.originalEvent.pageY : e.originalEvent.changedTouches[0].pageY;
        camera.position.set(80, 80, 80); //カメラを元の位置に戻す
        $contentFlickData = {};
    });
});

})();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console