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

              
                <!-- https://bountify.co/printing-and-saving-2-canvases-simultaneously -->

<div>
  <button id="save">Save as PNG</button>
<button id="print">Print Canvases as PDF</button>
Load JSON: <input type="file" id="jsonload" />
<button id='canvas2json'>Create + Save JSON as File</button>
<button id='loadJson2Canvas'>Load JSON Text Area to Canvas</button>
<button onclick="changeView(1);"><span id="viewText">(Front)</span></button>
<button onclick="dropText();">Drop Text</button>
<button onclick="toggleFrontPreview();">Toggle Front Preview</button>
<button onclick="toggleBackPreview();">Toggle Back Preview</button>
</div>
<div class="mainCanvasArea">
  <canvas id="canvas1" width="400" height="400"></canvas>
  <canvas id="canvas2" width="400" height="400"></canvas>
</div>
<div class="previewCanvasArea">
  <canvas class="invisible" id="previewCanvas1" width="400" height="400"></canvas>
  <canvas class="invisible" id="previewCanvas2" width="400" height="400"></canvas>
</div>
<div>
  <br> JSON Generated on Save:<br>
<textarea id='myTextArea' onfocus="this.select();" onmouseup="return false;"></textarea>
</div>
              
            
!

CSS

              
                canvas {
  border-radius: 3px;
  border: 1px solid #343A40;
  margin: auto;
  margin-top: 10px;
}
#myTextArea {
  width: 400px;
  height: 400px;
}
input[type='file'] {
  color: transparent;
  max-width: 78px;
}
.invisible {
  display: none;
}

.mainCanvasArea {
  text-align: center;
  display:inline-block;
}

.previewCanvasArea {
  display:inline-block;
  margin-left:150px;
}
              
            
!

JS

              
                var mainCanvas, canvas1, canvas2, padding = 10, view = false;
var previewCanvas1, previewCanvas2;

function dropText() {
  var text = new fabric.IText('test');
  mainCanvas.add(text);
}

function changeView(value) {
  if (view) {
    $viewText.textContent = "Back";
    mainCanvas = canvas1;
    $('#canvas1').parent().css('display', '');
    $('#canvas2').parent().css('display', 'none');
  } else {
    $viewText.textContent = "Front";
    mainCanvas = canvas2;
    $('#canvas1').parent().css('display', 'none');
    $('#canvas2').parent().css('display', '');
  }
  view = !view;
}

function toggleFrontPreview() {
  $('#previewCanvas1').toggleClass('invisible');
}

function toggleBackPreview() {
  $('#previewCanvas2').toggleClass('invisible');
}

// Save as image
function download(url, name) {
  $("<a>")
    .attr({
    href: url,
    download: name
  })[0]
    .click();
}

function save() {
  mainCanvas.deactivateAll().renderAll();
  var c = document.getElementById('canvas1');
  var dataURL = c.toDataURL();
  var name = 'image';
  download(dataURL, name + ".png");
  var c = document.getElementById('canvas2');
  var dataURL = c.toDataURL();
  var name = 'image';
  download(dataURL, name + ".png");
}

function onCanvasModified(option, previewCanvas) {
  var ob = option.target;
  var index = mainCanvas.getObjects().indexOf(ob);
  ob.clone(function(obj) {
    previewCanvas.insertAt(obj, index, true);
  });
};

$(function(){

  $viewText = document.querySelector("#viewText");
  canvas1 = new fabric.Canvas('canvas1');
  canvas1.on('object:added', option => onCanvasModified(option, previewCanvas1));
  canvas1.on('object:modified',  option => onCanvasModified(option, previewCanvas1));
  canvas1.on('object:scaling',  option => onCanvasModified(option, previewCanvas1));
  canvas1.on('object:moving',  option => onCanvasModified(option, previewCanvas1));
  canvas2 = new fabric.Canvas('canvas2');
  canvas2.on('object:added', option => onCanvasModified(option, previewCanvas2));
  canvas2.on('object:modified',  option => onCanvasModified(option, previewCanvas2));
  canvas2.on('object:scaling',  option => onCanvasModified(option, previewCanvas2));
  canvas2.on('object:moving',  option => onCanvasModified(option, previewCanvas2));
  previewCanvas1 = new fabric.StaticCanvas('previewCanvas1');
  previewCanvas2 = new fabric.StaticCanvas('previewCanvas2');
  changeView(1);

  // canvas2json
  $("#canvas2json").click(function() {
    var json = {};
    json.c1 = canvas1.toJSON(['selectable']);
    json.c2 = canvas2.toJSON(['selectable']);
    $("#myTextArea").text(JSON.stringify(json));
    var a = document.createElement("a");
    var file = new Blob([JSON.stringify(json)], {
      type: 'text/plain'
    });
    a.href = URL.createObjectURL(file);
    a.download = 'json.txt';
    a.click();
  });

  // load json2canvas
  $("#loadJson2Canvas").click(function() {
    var dataToLoad = JSON.parse($("#myTextArea").val());
    canvas1.loadFromJSON(
      dataToLoad.c1,
      canvas1.renderAll.bind(canvas1));
    canvas2.loadFromJSON(
      dataToLoad.c2,
      canvas2.renderAll.bind(canvas2));
  });

  $('#jsonload').change(function(e) {
    var file = e.target.files[0];
    if (!file) return;
    var reader = new FileReader();
    reader.onload = function(f) {
      var data = f.target.result;
      data = JSON.parse(data);

      canvas1.loadFromJSON(
        data.c1,
        canvas1.renderAll.bind(canvas1));
      canvas2.loadFromJSON(
        data.c2,
        canvas2.renderAll.bind(canvas2));
    };
    reader.readAsText(file);
  });
  $(this).val(null);

  // Bleed area
  mainCanvas.on('object:moving', function(e) {
    var obj = e.target;

    // if object is too big ignore
    if (obj.currentHeight > obj.canvas.height - padding * 2 ||
        obj.currentWidth > obj.canvas.width - padding * 2) {
      return;
    }
    obj.setCoords();

    // top-left corner
    if (obj.getBoundingRect().top < padding ||
        obj.getBoundingRect().left < padding) {
      obj.top = Math.max(obj.top, obj.top - obj.getBoundingRect().top + padding);
      obj.left = Math.max(obj.left, obj.left - obj.getBoundingRect().left + padding);
    }

    // bot-right corner
    if (obj.getBoundingRect().top + obj.getBoundingRect().height > obj.canvas.height - padding ||
        obj.getBoundingRect().left + obj.getBoundingRect().width > obj.canvas.width - padding) {
      obj.top = Math.min(
        obj.top,
        obj.canvas.height - obj.getBoundingRect().height + obj.top - obj.getBoundingRect().top - padding);
      obj.left = Math.min(
        obj.left,
        obj.canvas.width - obj.getBoundingRect().width + obj.left - obj.getBoundingRect().left - padding);
    }
  });		

  $("#save").click(save);

  // Print
  $("#print").click(function () {
    mainCanvas.deactivateAll().renderAll();
    var dataUrl1 = document.getElementById('canvas1').toDataURL();
    var dataUrl2 = document.getElementById('canvas2').toDataURL();
    var windowContent = '<!DOCTYPE html>';
    windowContent += '<html>';
    windowContent += '<head><title>Print</title>';
    windowContent += '<style>@media print{.page-break{display:block; page-break-before:always;}}</style>';
    windowContent += '</head>';
    windowContent += '<body>';
    windowContent += '<img src="' + dataUrl1 + '" onload=window.print();window.close();>';
    windowContent += '<div class="page-break"></div>';
    windowContent += '<img src="' + dataUrl2 + '" onload=window.print();window.close();>';
    windowContent += '</body>';
    windowContent += '</html>';
    var printWin = window.open('', '', 'width=340,height=260');
    printWin.document.open();
    printWin.document.write(windowContent);
  });

});


              
            
!
999px

Console