diff --git a/source/_posts/canvas.md b/source/_posts/canvas.md new file mode 100644 index 0000000..10872fc --- /dev/null +++ b/source/_posts/canvas.md @@ -0,0 +1,242 @@ +--- +title: HTML Canvas +date: 2024-05-28 22:16:42 +background: bg-[#cc5534] +tags: + - web +categories: + - Programming +intro: | + This HTML Canvas quick reference cheat sheet lists the common HTML5 Canvas design tags in readable layout. +plugins: + - copyCode +--- + +## Geting Started + +### Basic Setup + +```html + + + + Canvas Example + + + + + + +``` + +### Getting the Context + +```js +const canvas = document.getElementById("myCanvas"); +const ctx = canvas.getContext("2d"); +``` + +## Drawing Shapes + +### Rectangles + +```js +ctx.fillStyle = "red"; +ctx.fillRect(10, 10, 150, 100); // x, y, width, height + +ctx.strokeStyle = "blue"; +ctx.lineWidth = 5; +ctx.strokeRect(200, 10, 150, 100); // x, y, width, height + +ctx.clearRect(15, 15, 30, 30); // x, y, width, height +``` + +## Paths + +### Lines + +```js +ctx.beginPath(); +ctx.moveTo(50, 50); // Starting point +ctx.lineTo(200, 50); // Ending point +ctx.lineTo(200, 200); // Next line ending point +ctx.closePath(); // Connects the end point to the start point +ctx.stroke(); +``` + +### Circles + +```js +ctx.beginPath(); +ctx.arc(150, 150, 75, 0, 2 * Math.PI); // x, y, radius, startAngle, endAngle +ctx.fillStyle = "green"; +ctx.fill(); +ctx.stroke(); +``` + +### Arcs + +```js +ctx.beginPath(); +ctx.arc(150, 150, 75, 0, Math.PI); // x, y, radius, startAngle, endAngle +ctx.stroke(); +``` + +## Bezier and Quadratic Curves + +### Quadratic Curve + +```js +ctx.beginPath(); +ctx.moveTo(50, 250); +ctx.quadraticCurveTo(200, 100, 400, 250); // cpX, cpY, endX, endY +ctx.stroke(); +``` + +### Bezier Curve + +```js +ctx.beginPath(); +ctx.moveTo(50, 300); +ctx.bezierCurveTo(150, 100, 350, 500, 450, 300); // cp1X, cp1Y, cp2X, cp2Y, endX, endY +ctx.stroke(); +``` + +### Text + +```js +ctx.font = "30px Arial"; +ctx.fillStyle = "black"; +ctx.fillText("Hello Canvas", 10, 50); // text, x, y + +ctx.strokeText("Hello Canvas", 10, 100); // text, x, y +``` + +### Images + +```js +const img = new Image(); +img.src = "path/to/image.jpg"; +img.onload = () => { + ctx.drawImage(img, 10, 10); // img, x, y + ctx.drawImage(img, 50, 50, 100, 100); // img, x, y, width, height + ctx.drawImage(img, 100, 100, 100, 100, 150, 150, 200, 200); // img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight +}; +``` + +## Transformations + +### Translation + +```js +ctx.translate(100, 100); // x, y +ctx.fillRect(0, 0, 50, 50); +``` + +### Rotation + +```js +ctx.rotate((Math.PI / 180) * 45); // Angle in radians +ctx.fillRect(100, 100, 50, 50); +``` + +### Scaling + +```js +ctx.scale(2, 2); // x, y +ctx.fillRect(50, 50, 50, 50); +``` + +## Gradients + +### Linear Gradient + +```js +const linearGradient = ctx.createLinearGradient(0, 0, 200, 0); // x0, y0, x1, y1 +linearGradient.addColorStop(0, "red"); +linearGradient.addColorStop(1, "blue"); +ctx.fillStyle = linearGradient; +ctx.fillRect(10, 10, 200, 100); +``` + +### Radial Gradient + +```js +const radialGradient = ctx.createRadialGradient(75, 50, 5, 90, 60, 100); // x0, y0, r0, x1, y1, r1 +radialGradient.addColorStop(0, "red"); +radialGradient.addColorStop(1, "blue"); +ctx.fillStyle = radialGradient; +ctx.fillRect(10, 10, 200, 100); +``` + +### Patterns + +```js +const img = new Image(); +img.src = "path/to/image.jpg"; +img.onload = () => { + const pattern = ctx.createPattern(img, "repeat"); // 'repeat', 'repeat-x', 'repeat-y', 'no-repeat' + ctx.fillStyle = pattern; + ctx.fillRect(0, 0, 300, 300); +}; +``` + +### Shadows + +```js +ctx.shadowColor = "rgba(0, 0, 0, 0.5)"; +ctx.shadowBlur = 10; +ctx.shadowOffsetX = 5; +ctx.shadowOffsetY = 5; + +ctx.fillStyle = "red"; +ctx.fillRect(100, 100, 100, 100); +``` + +## Compositing + +### Global Alpha + +```js +ctx.globalAlpha = 0.5; +ctx.fillStyle = "red"; +ctx.fillRect(100, 100, 100, 100); + +ctx.fillStyle = "blue"; +ctx.fillRect(150, 150, 100, 100); +``` + +### Global Composite Operation + +```js +ctx.globalCompositeOperation = "source-over"; // Default +ctx.fillStyle = "red"; +ctx.fillRect(100, 100, 100, 100); + +ctx.globalCompositeOperation = "destination-over"; +ctx.fillStyle = "blue"; +ctx.fillRect(150, 150, 100, 100); +``` + +### Animations + +```js +let x = 0; +function draw() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.fillStyle = "blue"; + ctx.fillRect(x, 100, 50, 50); + x += 2; + requestAnimationFrame(draw); +} +draw(); +``` + +## Also read + +- [MDN doc ](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) diff --git a/source/_posts/css3.md b/source/_posts/css3.md index e99e4d3..3c7b940 100644 --- a/source/_posts/css3.md +++ b/source/_posts/css3.md @@ -1034,7 +1034,7 @@ html { ## Modern CSS -### CONTAINER QUERIES (SIZE) +### container queries(size) ```css .element-wrap { @@ -1048,7 +1048,7 @@ html { } ``` -### CONTAINER QUERIES (STYLE) +### container qeries(style) ```css .container { @@ -1074,7 +1074,7 @@ html { } ``` -### CONTAINER UNITS +### container units - The units are cqw (“container query width”), - cqh (“container query height”), @@ -1097,7 +1097,7 @@ h2 { } ``` -### THE :HAS() PSEUDO SELECTOR +### the :has() pseudo selector ```css figure:has(figcaption) { @@ -1106,7 +1106,7 @@ figure:has(figcaption) { } ``` -### NESTING +### nesting ```css .cards { @@ -1123,7 +1123,7 @@ figure:has(figcaption) { ### -### SCOPING @scope +### scoping ```css @scope { @@ -1145,7 +1145,7 @@ figure:has(figcaption) { } ``` -### CASCADE LAYERS +### cascade layers ```css /* Specify the order to apply styles in cascade */ @@ -1164,7 +1164,7 @@ figure:has(figcaption) { } ``` -### LOGICAL PROPERTIES +### logical properties ```css button { @@ -1186,7 +1186,7 @@ button { } ``` -### P3 COLORS +### p3 colors ```html
@@ -1250,7 +1250,7 @@ button {
``` -### COLOR MIXING +### color mixing ```css .swatch { @@ -1273,7 +1273,7 @@ button { } ``` -### MARGIN TRIM +### margin trim ```css .container { @@ -1287,7 +1287,7 @@ button { } ``` -### TEXT WRAPPING +### text wrapping ```css .balance { @@ -1307,7 +1307,7 @@ main { } ``` -### SUBGRID +### subgrid ```css .grid { diff --git a/source/_posts/typescript.md b/source/_posts/typescript.md new file mode 100644 index 0000000..50fa8a7 --- /dev/null +++ b/source/_posts/typescript.md @@ -0,0 +1,330 @@ +--- +title: TypeScript +date: 2024-05-28 22:12:25 +background: bg-[#ebd94e] +tags: + - ts + - web +categories: + - Programming +intro: | + A TypeScript cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners. +plugins: + - copyCode +--- + +## Getting Started + +### Installing the Compiler + +```shell +npm install typescript --save-dev +npm tsc +``` + +## Basic DataTypes + +### basic types + +```ts +let isDone: boolean = false; +let age: number = 30; +let userName: string = "John"; +let list: number[] = [1, 2, 3]; +let tuple: [string, number] = ["hello", 10]; +let notSure: any = 4; +notSure = "maybe a string instead"; +``` + +### enums + +```ts +enum Color { + Red, + Green, + Blue, +} +let c: Color = Color.Green; +``` + +### interface + +```ts +interface Person { + firstName: string; + lastName: string; + age?: number; // Optional property +} + +function greet(person: Person) { + return "Hello, " + person.firstName + " " + person.lastName; +} +``` + +### Functions + +```ts +function add(x: number, y: number): number { + return x + y; +} + +let myAdd = function (x: number, y: number): number { + return x + y; +}; + +let myArrowAdd = (x: number, y: number): number => x + y; + +function buildName(firstName: string, lastName = "Smith") { + return firstName + " " + lastName; +} + +function buildFullName(firstName: string, ...restOfName: string[]) { + return firstName + " " + restOfName.join(" "); +} +``` + +### Classes + +```ts +class Greeter { + greeting: string; + constructor(message: string) { + this.greeting = message; + } + greet() { + return "Hello, " + this.greeting; + } +} + +let greeter = new Greeter("world"); +``` + +### Inheritance + +```ts +class Animal { + move(distance: number = 0) { + console.log(`Animal moved ${distance} meters.`); + } +} + +class Dog extends Animal { + bark() { + console.log("Woof! Woof!"); + } +} + +const dog = new Dog(); +dog.bark(); +dog.move(10); +dog.bark(); +``` + +### Generics + +```ts +function identity(arg: T): T { + return arg; +} + +let output1 = identity("myString"); +let output2 = identity(42); +``` + +### Type Assertions + +```ts +let someValue: any = "this is a string"; +let strLength: number = (someValue).length; +// or +let strLength2: number = (someValue as string).length; +``` + +## Modules + +### Export + +```ts +export interface StringValidator { + isAcceptable(s: string): boolean; +} + +export class ZipCodeValidator implements StringValidator { + isAcceptable(s: string) { + return s.length === 5; + } +} +``` + +### Import + +```ts +import { ZipCodeValidator } from "./ZipCodeValidator"; + +let myValidator = new ZipCodeValidator(); +``` + +### Namespaces + +```ts +namespace Validation { + export interface StringValidator { + isAcceptable(s: string): boolean; + } + + export class LettersOnlyValidator implements StringValidator { + isAcceptable(s: string) { + return /^[A-Za-z]+$/.test(s); + } + } +} + +let validator = new Validation.LettersOnlyValidator(); +``` + +## Union and Intersection Types + +### Union Types + +```ts +function padLeft(value: string, padding: string | number) { + if (typeof padding === "number") { + return Array(padding + 1).join(" ") + value; + } + if (typeof padding === "string") { + return padding + value; + } + throw new Error(`Expected string or number, got '${padding}'.`); +} +``` + +### Intersection Types + +```ts +interface ErrorHandling { + success: boolean; + error?: { message: string }; +} + +interface ArtworksData { + artworks: { title: string }[]; +} + +type ArtworksResponse = ArtworksData & ErrorHandling; + +const response: ArtworksResponse = { + success: true, + artworks: [{ title: "Mona Lisa" }], +}; +``` + +## Utility Types + +### Partial + +```ts +interface User { + id: number; + name: string; + age: number; +} + +let partialUser: Partial = { + name: "Alice", +}; +``` + +### Readonly + +```ts +let readonlyUser: Readonly = { + id: 1, + name: "Bob", + age: 25, +}; + +// readonlyUser.age = 26; // Error: cannot reassign a readonly property +``` + +### Pick + +```ts +type UserName = Pick; + +let userName: UserName = { + name: "Charlie", +}; +``` + +### Omit + +```ts +type UserWithoutAge = Omit; + +let userWithoutAge: UserWithoutAge = { + id: 2, + name: "Dave", +}; +``` + +## Decorators + +### Class Decorator + +```ts +function sealed(constructor: Function) { + Object.seal(constructor); + Object.seal(constructor.prototype); +} + +@sealed +class Greeter { + greeting: string; + constructor(message: string) { + this.greeting = message; + } + greet() { + return "Hello, " + this.greeting; + } +} +``` + +### Method Decorator + +```ts +function enumerable(value: boolean) { + return function ( + target: any, + propertyKey: string, + descriptor: PropertyDescriptor + ) { + descriptor.enumerable = value; + }; +} + +class Greeter { + greeting: string; + constructor(message: string) { + this.greeting = message; + } + + @enumerable(false) + greet() { + return "Hello, " + this.greeting; + } +} +``` + +### Async/Await + +```ts +async function fetchData(url: string) { + let response = await fetch(url); + let data = await response.json(); + return data; +} +``` + +## Also read + +- [TypeScript](https://www.typescriptlang.org/docs/) diff --git a/source/assets/icon/canvas.svg b/source/assets/icon/canvas.svg new file mode 100644 index 0000000..2a5bd78 --- /dev/null +++ b/source/assets/icon/canvas.svg @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +image/svg+xml + + + + + +2021 + + + + + +Timothée Giet + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/source/assets/icon/typescript.svg b/source/assets/icon/typescript.svg new file mode 100644 index 0000000..c332ad5 --- /dev/null +++ b/source/assets/icon/typescript.svg @@ -0,0 +1,2 @@ + +file_type_typescript \ No newline at end of file