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

              
                <body>
  <div class="wrapper">
    <h2>Translate Test</h2>
    <p>
      Canvas中央に描画してから、translate(0,0);
      をする。
    </p>
    <p>
     <span class="red">赤:最初に描画した四角形</span> <br>
      <span class="blue">青:canvas中央へ移動してから、translate(0,0)した四角形</span>
     
    </p>
    <div class="box">
      <h3>1. (0,0) から描画して基準点変更(regX = regY = 25) </h3>
      <canvas id="canv-1" width="150" height="150">Canvas対応ブラウザを使って下さい。</canvas>
    </div>
    <div class="box">
      <h3>2. (0,0) を中心にして描画(regX = regY = 0)</h3>
      <canvas id="canv-2" width="150" height="150">Canvas対応ブラウザを使って下さい。</canvas>
    <div>
  </div>
  
  <script src="https://code.createjs.com/easeljs-0.8.1.min.js"></script>
  <script src="https://code.createjs.com/preloadjs-0.6.1.min.js"></script>
  <script src="https://code.createjs.com/tweenjs-0.6.1.min.js"></script>
</body>

              
            
!

CSS

              
                .wrapper {
  width: 100%;
  margin-right: auto;
  margin-left: auto;
}

.box{
  display: inline-block;
  text-align: center;
  width: 40%;
}

canvas {
  border: 1px solid #333;
}

.red{
  color: hsl(0, 80%, 70%);
}

.blue{
  color: hsl(240, 80%, 70%);
}
              
            
!

JS

              
                window.onload = function(){
  var stage1;
  var stage2;
  var rect_w = 50;
  var rect_h = 50;
  
  (function(){
    initialize();
    
    function initialize() {
      var canvas = document.getElementById('canv-1');
      var canvas_w = canvas.width;
      var canvas_h = canvas.height;
      stage1 = new createjs.Stage(canvas);

      var g_rect = getGraphicsRect(1, '#333', 'hsl(0, 80%, 80%)', 0, 0, rect_w, rect_h);
      var rect = new createjs.Shape(g_rect);
      stage1.addChild(rect);

      var rect_trans = rect.clone();
      var g = rect.graphics.clone();
      g.beginFill('hsl(240, 80%, 80%)')
       .drawRect(0, 0, rect_w, rect_h);
      rect_trans.graphics = g;

      rect_trans.regX = rect_w/2; 
      rect_trans.regY = rect_h/2;

      rect_trans.x = canvas_w/2; 
      rect_trans.y = canvas_h/2;

      //rect.scaleX = rect.scaleY = 1.5; 
     var matrix = rect_trans.getMatrix();
     console.log(matrix);
      matrix
        .translate(0, 0)
        .decompose(rect_trans);
      stage1.addChild(rect_trans);
      stage1.update(); 
     }
  })();
  
  (function(){
    initialize();
    
    function initialize() {
      var canvas = document.getElementById('canv-2');
      var canvas_w = canvas.width;
      var canvas_h = canvas.height;
      stage2 = new createjs.Stage(canvas);
     
      var g_rect = getGraphicsRect(1, "#000", 'hsl(0, 80%, 70%)', 
    -rect_w/2, -rect_h/2, rect_w, rect_h);
    
     var rect = new createjs.Shape(g_rect);
      stage2.addChild(rect);
      stage2.update();
      
      var rect_trans = rect.clone();
      var g = rect.graphics.clone();
      g.beginFill('hsl(240, 80%, 80%)')
        .drawRect(-rect_w/2, -rect_h/2, rect_w, rect_h);
      rect_trans.graphics = g;
      rect_trans.x = canvas_w/2; 
      rect_trans.y = canvas_h/2;
    
     var matrix = rect_trans.getMatrix();
      matrix
        .translate(0, 0)
        .decompose(rect_trans);
      stage2.addChild(rect_trans);
      stage2.update(); 
    }
  })();
  
  function getGraphicsRect(strokeStyle=1, stroke_color='#333', fill_color, 
  x, y, width, height){
    var g_rect = new createjs.Graphics()
    .setStrokeStyle(strokeStyle)
    .beginStroke(stroke_color)
    .beginFill(fill_color)
    .drawRect(x, y, width, height);
    return g_rect;
   }
};
              
            
!
999px

Console