git @ Cat's Eye Technologies yoob.js / master eg / canvas-resizer.html
master

Tree @master (Download .tar.gz)

canvas-resizer.html @masterraw · history · blame

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>yoob.CanvasResizer Demo</title>
  <style>
html, body, article {
    width: 100%;
    margin: 0;
    padding: 0;
    line-height: 0;
    text-align: center;
}
header {
    background: goldenrod;
    line-height: 28px;
    text-align: left;
}
h1 {
    margin: 0;
    padding-top: 6px;
}
#message {
    line-height: 12px;
}
  </style>
</head>
<body>
  <header>
    <h1>yoob.Resizer Demo</h1>
    <button id='hide'>Hide header</button> (reload to bring it back)
  </header>
  <article>
    <p id="message">There is not enough space to show the canvas!</p>
    <canvas id="c1"></canvas>
  </article>
</body>
<script src="../src/yoob/element-factory.js"></script>
<script src="../src/yoob/canvas-resizer.js"></script>
<script type="text/javascript">
"use strict";
var header = document.getElementsByTagName('header')[0];
var canvas = document.getElementById('c1');
var ctx = c1.getContext('2d');

var cr = (new yoob.CanvasResizer()).init({
    canvas: canvas,
    onResizeEnd: function(newWidth, newHeight, changed) {
        if (!changed) return;
        ctx.fillStyle = 'red';
        ctx.fillRect(0, 0, canvas.width / 2, canvas.height / 2);
        ctx.fillStyle = 'blue';
        ctx.fillRect(canvas.width / 2, canvas.height / 2, canvas.width / 2, canvas.height / 2);
        ctx.beginPath();
        ctx.fillStyle = 'green';
        ctx.arc(canvas.width / 2, canvas.height / 2, canvas.height / 4, 0, 2 * Math.PI, false);
        ctx.fill();
    },
    desiredWidth: 640,
    desiredHeight: 400,
    missingCanvasElement: document.getElementById('message')
}).register();

document.getElementById('hide').onclick = function() {
    header.style.display = "none";
    cr.resizeCanvas();
};
yoob.makeCheckbox(header, true, "redimension canvas", function(b) {
    cr.redimensionCanvas = b;
    cr.resizeCanvas();
});
yoob.makeCheckbox(header, true, "preserve aspect ratio", function(b) {
    cr.preserveAspectRatio = b;
    cr.resizeCanvas();
});
yoob.makeCheckbox(header, false, "allow expansion", function(b) {
    cr.allowExpansion = b;
    cr.resizeCanvas();
});
yoob.makeCheckbox(header, true, "allow contraction", function(b) {
    cr.allowContraction = b;
    cr.resizeCanvas();
});
yoob.makeCheckbox(header, true, "center vertically", function(b) {
    cr.centerVertically = b;
    cr.resizeCanvas();
});

</script>