Skip to content

Instantly share code, notes, and snippets.

View sandrabosk's full-sized avatar
👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall

Aleksandra Bošković sandrabosk

👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall
  • Ironhack
View GitHub Profile
@sandrabosk
sandrabosk / index.js
Last active April 27, 2020 13:30
solution-canvas-image-text
console.log('I am connected!');
const theCanvas = document.getElementById('canvas');
const ctx = theCanvas.getContext('2d');
// create new image object
const img = new Image();
// "src" has to point as the image is used in HTML file
img.src =

Check for understanding - images and text

  1. Setup:
  • Create following folder/file structure:
.
├── index.html
└── js
 └── index.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Canvas - Pattern and Shadow</title>
</head>
<body>

How to use images on canvas?

Step 1: create image tag withnew Image()

We will start with creating image tag, and the following line of code does exactly that:

let naturePic = new Image();
console.log("what is this: ", naturePic);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Canvas Animations</title>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Canvas Animations</title>
</head>
<body>

Canvas intro - check for understanding

Let´s start with some basic shapes! First, we will try the create the RSS symbol. Something like this:

Steps:

  1. Create folder and all the files:
// ************************************************************************************
// https://www.codewars.com/kata/56747fd5cb988479af000028/train/javascript
// You are going to be given a word. Your job is to return the middle character of the word.
// If the word's length is odd, return the middle character. If the word's length is even,
// return the middle 2 characters.
// Examples:
// getMiddle("test") should return "es"
// getMiddle("testing") should return "t"
// getMiddle("middle") should return "dd"
/*
FOR TEST EXAMPLES AND DIFFERENT MATCHERS (`toEqual`, `toBe`, `not.toBe`, etc):
https://jasmine.github.io/api/3.4/global
*/
describe('centsToDecimals function', () => {
// Describes the suite / group of tests
// Use this test suite as a starting point/reference.
// 1: What is the expected output? And why?
function print() {
console.log(1);
setTimeout(() => console.log(2), 1000);
setTimeout(() => console.log(3), 0);
console.log(4);
}
print(); // => ???