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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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

              
                <canvas id="demo" width="480" height="360"></canvas>
<div>
  <h1>(Attempt to) Center the SVG:</h1>
  <input id="scale" value="1"/>
  <button id="setscale">Change Scale</button><br/>
  <button id="method1">Method 1</button>
  <button id="method2">Method 2</button>
  <button id="method3">Method 3</button>
  <button id="method4">Method 4</button>
  <button id="method5">Method 5</button>
  <button id="resetbtn">Reset</button>
</div>
<p>
  Method 1 works for raster graphics, but not rendered SVG graphics. Method 2 works for SVGs, but not raster graphics. The other three don't produce the desired outcome for either of the graphics.
</p>
              
            
!

CSS

              
                body{
  font-family:sans-serif;
  line-height:1.8em;
}
canvas{
  border: 5px solid black;
}
h1{
  font-size: 1.5em;
}
              
            
!

JS

              
                var stage = new createjs.Stage('demo');

//forget about getting the stage size dynamically for now
var width = 480;
var height = 360;

//add svg to stage (straight-up *DOESN'T WORK IN FIREFOX* right now)
var bitmap = new createjs.Bitmap('https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg');
bitmap.scale = 1;
stage.addChild(bitmap);

//add raster graphic for comparison
var bitmap2 = new createjs.Bitmap('https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png');
bitmap2.scale = .25;
stage.addChild(bitmap2);

stage.update();

/**
* Below are my various attempts at centering both SVG and raster graphics in CreateJS
*/

function method1(bitmap){
  let bounds = bitmap.getBounds();
  bitmap.regX = bounds.width / 2;
  bitmap.regY = bounds.height / 2;
  bitmap.x = width / 2;
  bitmap.y = height / 2;
  stage.update();
}

function method2(bitmap){
  //assuming the SVG is square...
  bitmap.regX = Math.min(width,height) / 2;
  bitmap.regY = Math.min(width,height) / 2;
  bitmap.x = width / 2;
  bitmap.y = height / 2;
  stage.update();
}

function method3(bitmap){
  let bounds = bitmap.getBounds();
  let ltg = bitmap.localToGlobal(bounds.width, bounds.height)
  bitmap.regX = ltg.x / 2;
  bitmap.regY = ltg.y / 2;
  bitmap.x = width / 2;
  bitmap.y = height / 2;
  stage.update();
}

function method4(bitmap){
  let bounds = bitmap.getTransformedBounds();
  bitmap.regX = bounds.width / 2;
  bitmap.regY = bounds.height / 2;
  bitmap.x = width / 2;
  bitmap.y = height / 2;
  stage.update();
}

function method5(bitmap){
  let bounds = bitmap.getTransformedBounds();
  let ltg = bitmap.localToGlobal(bounds.width, bounds.height)
  bitmap.regX = ltg.x / 2;
  bitmap.regY = ltg.y / 2;
  bitmap.x = width / 2;
  bitmap.y = height / 2;
  stage.update();
}

//-----------------------------------------------------------------

document.getElementById('method1').onclick = function(event){
  method1(bitmap);
  method1(bitmap2);
}

document.getElementById('method2').onclick = function(event){
  method2(bitmap);
  method2(bitmap2);
}

document.getElementById('method3').onclick = function(event){
  method3(bitmap);
  method3(bitmap2);
}

document.getElementById('method4').onclick = function(event){
  method4(bitmap);
  method4(bitmap2);
}

document.getElementById('method5').onclick = function(event){
  method5(bitmap);
  method5(bitmap2);
}

document.getElementById('setscale').onclick = function(event){
  bitmap.scale = parseFloat(document.getElementById('scale').value);
  stage.update();
}

document.getElementById('resetbtn').onclick = function(event){
  bitmap.x = bitmap.y = bitmap.regX = bitmap.regY = 0;
  bitmap2.x = bitmap2.y = bitmap2.regX = bitmap2.regY = 0;
  stage.update();
}
              
            
!
999px

Console