SHOW:
|
|
- or go back to the newest paste.
| 1 | // we create variables to access key HTML elements in the project | |
| 2 | //we create them as constants because we won't be assigning other values to them anymore | |
| 3 | const choosenPicture = document.querySelector("#select-picture");
| |
| 4 | const canvas = document.querySelector("#meme");
| |
| 5 | const textTop = document.querySelector("#text-top");
| |
| 6 | const textBottom = document.querySelector("#text-bottom");
| |
| 7 | const downloadButton = document.querySelector('#download-button');
| |
| 8 | //global variable storing our selected image as a URL | |
| 9 | let picture; | |
| 10 | ||
| 11 | //we hide the download button | |
| 12 | downloadButton.style.display='none'; | |
| 13 | ||
| 14 | // On the button where we select images we set the so-called event observer whose task will be to monitor each change of this element | |
| 15 | ||
| 16 | // what this means for us is that when we select an image, the code for the event will be executed | |
| 17 | ||
| 18 | // the e(event) parameter passes information about the event and the element | |
| 19 | choosenPicture.addEventListener("change", function (e) {
| |
| 20 | // we create a string (URL) that will represent our image object and this will facilitate its use in the code | |
| 21 | // this method takes a file as a parameter, we use the e parameter to return the first picture we selected | |
| 22 | const pictureUrl = URL.createObjectURL(e.target.files[0]); | |
| 23 | ||
| 24 | // this constructor creates a new html element for us - <img>. | |
| 25 | picture = new Image(); | |
| 26 | // we set the path to the image | |
| 27 | picture.src = pictureUrl; | |
| 28 | ||
| 29 | // load the image when selected in the canvas | |
| 30 | picture.addEventListener("load", function () {
| |
| 31 | // test add information in the console to check if the picture was loaded correctly | |
| 32 | console.log("Loading the image...");
| |
| 33 | updateMeme(canvas, picture, textTop.value,textBottom.value); | |
| 34 | }); | |
| 35 | downloadButton.style.display='block'; | |
| 36 | }); | |
| 37 | ||
| 38 | //function responsible for loading the canvas picture and adding the top and bottom caption | |
| 39 | ||
| 40 | function updateMeme(canvas, picture, textTop, textBottom) {
| |
| 41 | //set the rendering context in the canvas in our case it will be 2D | |
| 42 | const ctx = canvas.getContext("2d");
| |
| 43 | //we set the width and height of the canva to the image parameters | |
| 44 | ||
| 45 | //uploading small images will end up bad quality | |
| 46 | const canvasWidth = picture.width; | |
| 47 | const canvasHeight = picture.height; | |
| 48 | // The font size will depend on the width of the picture if you want to change it, you can edit the value here | |
| 49 | // Math.floor rounds to the smallest or largest integer | |
| 50 | ||
| 51 | const fontSize = Math.floor(canvasWidth / 20); | |
| 52 | // the spacing of the captions from the top and bottom edges of the image the smaller the value, the closer the captions will be to the center | |
| 53 | const offsetY = canvasHeight / 25; | |
| 54 | ||
| 55 | //set the width and height of our canvas to the size of the image | |
| 56 | canvas.width = canvasWidth; | |
| 57 | canvas.height = canvasHeight; | |
| 58 | //this method draws the image in the canvas cord 0 , 0 indicate where to start drawing the left - top corner of the canvas | |
| 59 | ctx.drawImage(picture, 0, 0); | |
| 60 | ||
| 61 | // text preparation https://www.w3schools.com/tags/ref_canvas.asp | |
| 62 | ||
| 63 | // color of letter borders | |
| 64 | ctx.strokeStyle = "black"; | |
| 65 | // width of the letter border | |
| 66 | ctx.lineWidth = Math.floor(fontSize / 4); | |
| 67 | //the color of the letter fill | |
| 68 | ctx.fillStyle = "white"; | |
| 69 | //centering of the text | |
| 70 | ctx.textAlign = "center"; | |
| 71 | //rounding of the border | |
| 72 | ctx.lineJoin = "round"; | |
| 73 | ctx.font = `${fontSize}px Summer`;
| |
| 74 | ||
| 75 | //setting the top text | |
| 76 | //set the baseline from which we start drawing the text | |
| 77 | ctx.textBaseline = "top"; | |
| 78 | //we draw the text without filling | |
| 79 | ctx.strokeText(textTop, canvasWidth / 2, offsetY); | |
| 80 | //we add a fill | |
| 81 | ctx.fillText(textTop, canvasWidth / 2, offsetY); | |
| 82 | ||
| 83 | // prepare the bottom text | |
| 84 | ctx.textBaseline = "bottom"; | |
| 85 | ctx.strokeText(textBottom, canvasWidth / 2, canvasHeight - offsetY); | |
| 86 | ctx.fillText(textBottom, canvasWidth / 2, canvasHeight - offsetY); | |
| 87 | } | |
| 88 | ||
| 89 | //for any change in the top text input, we refresh the canvas image | |
| 90 | textTop.addEventListener("change", function () {
| |
| 91 | updateMeme(canvas, picture, textTop.value, textBottom.value); | |
| 92 | }); | |
| 93 | ||
| 94 | textBottom.addEventListener("change", function () {
| |
| 95 | updateMeme(canvas, picture, textTop.value, textBottom.value); | |
| 96 | }); | |
| 97 | ||
| 98 | // OPTIONAL button to download image | |
| 99 | // We convert the canvas to an image when the download button is clicked | |
| 100 | downloadButton.addEventListener("click", function() {
| |
| 101 | ||
| 102 | ||
| 103 | const dataURL = canvas.toDataURL("image/jpeg");
| |
| 104 | ||
| 105 | ||
| 106 | // we create a new <a> tag with the path to our image set, | |
| 107 | const a = document.createElement('a');
| |
| 108 | a.href = dataURL; | |
| 109 | // we set the download attribute with the name of the downloaded file | |
| 110 | a.download = 'meme.jpeg'; | |
| 111 | // we call the click method on the newly created element as a result the photo will download automatically | |
| 112 | a.click(); | |
| 113 | }); | |
| 114 | ||
| 115 |