Your IP : 216.73.217.62


Current Path : /home/kamilrogam/poligon/projekty/jsMeme/assets/
Upload File :
Current File : /home/kamilrogam/poligon/projekty/jsMeme/assets/jsMeme.js

/*jshint esversion:6,browser:true,devel: true*/
document.addEventListener('DOMContentLoaded', function () {
	const canvas = document.getElementById('meme');
	const canvasBtnDownload = document.getElementById('btn-download');
	const canvasBtnGenerate = document.getElementById('btn-generate');
	const canvasLine1 = document.getElementById('line1');
	const canvasLine2 = document.getElementById('line2');
	const canvasPic = document.getElementById('img');
	const canvasBg = document.getElementById('color');
	const ctx = canvas.getContext('2d');

	const jsMemeBg = function (color) {
		ctx.fillStyle = color;
		ctx.fillRect(0, 0, canvas.width, canvas.height);
	};

	const jsMemeGenerate = function () {
		const imageObj = new Image();
		let X, Y, sourceWidth, sourceHeight, destWidth, destHeight;
		imageObj.src = canvasPic.value;
		X = Y = 10;
		sourceWidth = imageObj.width;
		sourceHeight = imageObj.height;
		destWidth = sourceWidth - 10;
		destHeight = sourceHeight - 100;
		ctx.drawImage(imageObj, X, Y, sourceWidth, sourceHeight, X, Y, destWidth, destHeight);

		ctx.font = "35px Arial";
		ctx.fillStyle = '#fff';
		ctx.textAlign = "center";
		ctx.fillText(canvasLine1.value.toUpperCase(), canvas.width / 2, canvas.height - 55);
		ctx.font = "25px Arial";
		ctx.fillText(canvasLine2.value.toUpperCase(), canvas.width / 2, canvas.height - 15);
	};

	jsMemeBg('#000');

	canvasBg.addEventListener('change', function () {
		jsMemeBg(canvasBg.value);
		jsMemeGenerate();
	});
	canvasBtnGenerate.addEventListener('click', function () {
		jsMemeGenerate();
	});
	canvasBtnDownload.addEventListener('click', function () {
		canvasBtnDownload.href = canvas.toDataURL();
	},false);
});