You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guide/Image-0900.html
+14-17Lines changed: 14 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -22,15 +22,15 @@
22
22
<p>The standard API for working with images on canvas is rather laborious, and often takes the fun out of creative coding. In Pts, the <ahref="#image-img"><code>Img</code></a> class simplifies the common use cases, from loading and displaying static images to generating dynamic textures, so that you can get started quickly. Let's take a look.</p>
23
23
<h3>Loading and Displaying Images</h3>
24
24
<p>We will start a minimalistic example: Load an image and display it on canvas. This can be done in 2 lines of code:</p>
<h5>Image credit: "C 50 Last Birds And Flowers" by Kurt Schwitters</h5>
30
-
<p>The above example uses the <em>static</em> function <ahref="#image-img"><code>Img.load</code></a>to load an image, and then uses CanvasForm's <ahref="#canvas-canvasform"><code>image</code></a> function to display it. The image will be displayed as soon as it's loaded.</p>
31
-
<p>To wait for the image to be ready first, either use the static <ahref="#image-img"><code>Img.loadAsync</code></a> function, or create a blank Img instance and then call the <em>instance</em> function <ahref="#image-img"><code>load</code></a>. An example:</p>
30
+
<p>The above example uses the <em>static</em> function <ahref="#image-img"><code>Img.load</code></a>, which returns a Promise that resolves to the loaded image, and then uses CanvasForm's <ahref="#canvas-canvasform"><code>image</code></a> function to display it. A load failure rejects the Promise.</p>
31
+
<p>You can also create an Img instance yourself and call the <em>instance</em> function <ahref="#image-img"><code>load</code></a>, which is handy when you want to configure the instance first. An example:</p>
32
32
<pre><code>(async function() {
33
-
let img = await Img.loadAsync( "/assets/img_demo.jpg" );
33
+
let img = await new Img().load( "/assets/img_demo.jpg" );
34
34
space.add( time => form.image( space.pointer, img ) );
35
35
})();
36
36
</code></pre>
@@ -40,11 +40,11 @@ <h5>In this example, we access the image's original width and height after it's
40
40
<h3>Editing Images</h3>
41
41
<p>When you create an Img instance with its <code>editable</code> parameter set to <code>true</code>, it will hold an internal canvas to support image manipulations. It will also match the pixel-density of your display. An example:</p>
42
42
<pre><code>// Create an editable img with the current space's pixelScale
43
-
let img = new Img( true, space.pixelScale );
43
+
let img = new Img( { editable: true, pixelScale: space.pixelScale } );
<p>You can do a lot with an editable image. Let's cover a couple common use cases.</p>
50
50
<h3>Get Pixels and Crop Regions</h3>
@@ -61,7 +61,7 @@ <h5>Click to cut out a region in the image. Move pointer to shift its position.<
61
61
<h3>Edit and Sync</h3>
62
62
<p>Since an editable <ahref="#image-img"><code>Img</code></a> stores an internal canvas, you can leverage <ahref="#canvas-canvasform"><code>CanvasForm</code></a>'s many drawing functions to draw directly on it. It's that easy!</p>
63
63
<p>After the image is loaded, you can access the canvas' rendering context through the property <code>img.ctx</code> and then create a new <ahref="#canvas-canvasform"><code>CanvasForm</code></a> instance with it. For example:</p>
@@ -76,7 +76,8 @@ <h5>Move pointer to draw patches on the image canvas.</h5>
76
76
<pre><code>// draw internal image canvas
77
77
form.image( img.canvas );
78
78
</code></pre>
79
-
<p>As we are only editing an internal canvas, the original image is unchanged until it's explicitly updated. Use <ahref="#image-img"><code>sync</code></a> to update the original image when needed.</p>
79
+
<p>As we are only editing an internal canvas, the original image is unchanged until it's explicitly updated. Use <ahref="#image-img"><code>sync</code></a>, which returns a Promise, to update the original image when needed: <code>await img.sync()</code>.</p>
80
+
<p>You can also work at the pixel level: <ahref="#image-img"><code>setPixel</code></a> writes a color into the cached pixel data, <ahref="#image-img"><code>updatePixels</code></a> applies those changes onto the canvas, and <ahref="#image-img"><code>loadPixels</code></a> refreshes the cache after you've drawn on the canvas directly. When you're done with an Img, call <ahref="#image-img"><code>dispose</code></a> to release its resources.</p>
80
81
<h3>Patterns</h3>
81
82
<p>In a similar way, you can treat an image (or an image canvas) as a pattern to fill an area. One difference is that we'll get a <ahref="https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern">CanvasPattern</a> instance for use in <code>form.fill(...)</code>, instead of an image for <code>form.image(...)</code>.</p>
82
83
<pre><code>const pattern = await Img.loadPattern( "tile.jpg", space );
@@ -103,19 +104,15 @@ <h3>Tips and Tricks</h3>
103
104
<h3>Cheatsheet</h3>
104
105
<p>Creating, loading, displaying</p>
105
106
<pre><code>// Simplest way
106
-
let img = Img.load( "demo.jpg");
107
+
let img = await Img.load( "demo.jpg");
107
108
108
109
// Load an editable image that matches the screen's resolution
109
-
// with an optional callback function when the image is loaded.
110
-
let img = Img.load("demo.png", true, space.pixelScale, onLoad );
Copy file name to clipboardExpand all lines: guide/md/_0900_Image.md
+15-17Lines changed: 15 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,21 +7,21 @@ The standard API for working with images on canvas is rather laborious, and ofte
7
7
We will start a minimalistic example: Load an image and display it on canvas. This can be done in 2 lines of code:
8
8
9
9
```
10
-
const img = Img.load( "/assets/demo.jpg" );
10
+
const img = await Img.load( "/assets/demo.jpg" );
11
11
space.add( time => form.image( space.pointer, img ) );
12
12
```
13
13
14
14

15
15
16
16
##### Image credit: "C 50 Last Birds And Flowers" by Kurt Schwitters
17
17
18
-
The above example uses the *static* function [`Img.load`](#image-img)to load an image, and then uses CanvasForm's [`image`](#canvas-canvasform) function to display it. The image will be displayed as soon as it's loaded.
18
+
The above example uses the *static* function [`Img.load`](#image-img), which returns a Promise that resolves to the loaded image, and then uses CanvasForm's [`image`](#canvas-canvasform) function to display it. A load failure rejects the Promise.
19
19
20
-
To wait for the image to be ready first, either use the static [`Img.loadAsync`](#image-img) function, or create a blank Img instance and then call the *instance* function [`load`](#image-img). An example:
20
+
You can also create an Img instance yourself and call the *instance* function [`load`](#image-img), which is handy when you want to configure the instance first. An example:
21
21
22
22
```
23
23
(async function() {
24
-
let img = await Img.loadAsync( "/assets/img_demo.jpg" );
24
+
let img = await new Img().load( "/assets/img_demo.jpg" );
25
25
space.add( time => form.image( space.pointer, img ) );
26
26
})();
27
27
```
@@ -38,11 +38,11 @@ When you create an Img instance with its `editable` parameter set to `true`, it
38
38
39
39
```
40
40
// Create an editable img with the current space's pixelScale
41
-
let img = new Img( true, space.pixelScale );
41
+
let img = new Img( { editable: true, pixelScale: space.pixelScale } );
42
42
img.load( "/assets/demo.jpg" ).then( ... );
43
43
44
-
// Alternatively, Img.loadAsync static function
45
-
let img2 = await Img.loadAsync( "/assets/demo.jpg", true, space.pixelScale );
44
+
// Alternatively, pass the options to the static load function
You can do a lot with an editable image. Let's cover a couple common use cases.
@@ -76,7 +76,7 @@ Since an editable [`Img`](#image-img) stores an internal canvas, you can leverag
76
76
After the image is loaded, you can access the canvas' rendering context through the property `img.ctx` and then create a new [`CanvasForm`](#canvas-canvasform) instance with it. For example:
77
77
78
78
```
79
-
const img = await Img.loadAsync( "demo.jpg" );
79
+
const img = await Img.load( "demo.jpg", true );
80
80
const imgForm = new CanvasForm( img.ctx );
81
81
...
82
82
imgForm.fill("#f00").rect( rect );
@@ -101,7 +101,9 @@ To display the edited image, use CanvasForm's [`image`](https://ptsjs.org/docs/?
101
101
form.image( img.canvas );
102
102
```
103
103
104
-
As we are only editing an internal canvas, the original image is unchanged until it's explicitly updated. Use [`sync`](#image-img) to update the original image when needed.
104
+
As we are only editing an internal canvas, the original image is unchanged until it's explicitly updated. Use [`sync`](#image-img), which returns a Promise, to update the original image when needed: `await img.sync()`.
105
+
106
+
You can also work at the pixel level: [`setPixel`](#image-img) writes a color into the cached pixel data, [`updatePixels`](#image-img) applies those changes onto the canvas, and [`loadPixels`](#image-img) refreshes the cache after you've drawn on the canvas directly. When you're done with an Img, call [`dispose`](#image-img) to release its resources.
0 commit comments