JSFiddle - React, Tailwind, and code Playground

by Arthur Lutkevichus

HTML

<canvas id="preview" style="background: #4e5465;">
  Your browser does not support the HTML5 canvas tag.
</canvas>

JavaScript

var canvas = document.getElementById("preview");
canvas.width = 800;
canvas.height = 600;
var ctx = canvas.getContext("2d");

// Draw background
var background = new Image();
//background.src = "https://static.pexels.com/photos/55787/pexels-photo-55787.jpeg"; // wide
background.src = "https://s-media-cache-ak0.pinimg.com/736x/d6/12/fe/d612fe57064516cc646889281f830230--wallpaper-for-iphone-iphone--wallpaper-phone-wallpapers.jpg"; // high

background.onload = function() {
  var maxWidth = canvas.width;
  var maxHeight = canvas.height;
  var ratio = 0;
  var width = background.width;
  var height = background.height;

  if (width > maxWidth) {
    ratio = maxWidth / width;
    height = Math.ceil(height * ratio);
    width = Math.ceil(width * ratio);
  }

  if (height > maxHeight) {
    ratio = maxHeight / height;
    width = Math.ceil(width * ratio);
    height = Math.ceil(height * ratio);
  }

  var left = maxWidth / 2 - width / 2;
  var top = maxHeight / 2 - height / 2;

  ctx.drawImage(background, left, top, width, height);

  // Draw subtitles
  var text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
  ctx.font = '22px sans-serif';
  ctx.textBaseline = "bottom";
  ctx.textAlign = "center";
  var textPadding = 15;
  var textMargin = 10;
  var textWidth = ctx.measureText(text).width + textPadding * 2;
  var textHeight = parseInt(ctx.font, 10) + textMargin * 2;
  var ellipsisWidth = ctx.measureText("…").width;
  
  while (textWidth >...