JSFiddle - React, Tailwind, and code Playground
HTML
<h2>Upload your ticket:</h2>
<h2><input type="file" id="imageLoader" name="imageLoader"/></h2>
<canvas id="imageCanvas"></canvas>
CSS
/* IMPORTING CUSTOM FONTS */
@font-face {
font-family: leixo;
src: url('fonts/leixo.ttf');
}
@font-face {
font-family: roboto;
src: url('fonts/roboto.ttf');
}
/*------------------------*/
/* EDITOR */
canvas {
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
width: 800px;
}
#imageCanvas {
background-color: lightgrey;
}
/*--------*/
/* MAIN */
html,
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background-color: #ededed;
/* prev: f2f2f2*/
font-family: sans-serif;
text-align: justify;
color: inherit;
}
#container {
min-height: 100%;
position: relative;
}
/*------*/
/* MAIN DIVIDERS */
#header {
padding: 1.5% 0 1.5% 0;
background: black;
border-bottom: solid #444444 4px;
text-align: right;
font-size: 90%;
}
#body {
padding-top: 2%;
padding-bottom: 14%;
/* Height of the footer (MUST BE AT LEAST THE SAME) */
}
#footer {
position: absolute;
padding: 1% 0 0 0;
bottom: 0;
width: 100%;
height: 14%;
/* Height of the footer (MUST BE AT LEAST THE SAME) */
background: black;
border-top: solid #444444 4px;
font-size: 80%;
text-align: center;
}
/*---------------*/
/* ELEMENT DIVIDER */
.elem {
padding-bottom: 50%;
background: green;
}
/*-----------------*/
/* BROWSER TITLE */
.title {
font-weight: lighter;
font-family: monospace;
font-size: 600%;
text-align: center;
}
/*---------------*/
/* MAIN TITLE */
h1 {
margin-top: 0%;
margin-bottom: 0%;
text-align: center;
font-weight: lighter;
font-family: leixo;
color: black;
font-size: 500%;
}
/*------------*/
/* CENTER LINE */
#centerLine {
border: none;
height: 2px;
width: 50%;
color: black;
background-color: black;
}
/*-------------*/
/* LOCATION TITLE */
#location {
text-align: center;
padding-bottom: 5%;
font-weight: lighter;
font-family: leixo;
color: #D63741;
font-size: 500%;
}
/*----------------*/
/*...
JavaScript
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
var canvas = document.getElementById('imageCanvas');
var ctx = canvas.getContext('2d');
function handleImage(e) {
var reader = new FileReader();
reader.onload = function(event) {
var img = new Image();
img.onload = function() {
canvas.width = window.innerWidth * 0.5;
canvas.height = window.innerHeight;
var hRatio = canvas.width / img.width;
var vRatio = canvas.height / img.height;
var ratio = Math.min(hRatio, vRatio);
var centerShift_x = (canvas.width - (img.width * ratio)) / 2;
var centerShift_y = (canvas.height - (img.height * ratio)) / 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, img.width, img.height,
centerShift_x, centerShift_y, img.width * ratio, img.height * ratio);
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
var isDrawing;
var rect = canvas.getBoundingClientRect();
var offsetX = rect.left;
var offsetY = rect.top;
canvas.onmousedown = function(e) {
isDrawing = true;
ctx.moveTo(e.clientX - offsetX, e.clientY - offsetY);
};
canvas.onmousemove = function(e) {
if (isDrawing) {
ctx.lineTo(e.clientX - offsetX, e.clientY - offsetY);
ctx.stroke();
}
};
canvas.onmouseup = function() {
isDrawing = false;
};