Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #935 from zbjornson/formats
Support canvas.getContext("2d", {alpha: boolean, pixelFormat: string})
- Loading branch information
Showing
23 changed files
with
1,052 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var Canvas = require('..') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var canvas = new Canvas(200, 200) | ||
var ctx = canvas.getContext('2d', {pixelFormat: 'A8'}) | ||
|
||
// Matches the "fillStyle" browser test, made by using alpha fillStyle value | ||
var palette = new Uint8ClampedArray(37 * 4) | ||
var i, j | ||
var k = 0 | ||
// First value is opaque white: | ||
palette[k++] = 255 | ||
palette[k++] = 255 | ||
palette[k++] = 255 | ||
palette[k++] = 255 | ||
for (i = 0; i < 6; i++) { | ||
for (j = 0; j < 6; j++) { | ||
palette[k++] = Math.floor(255 - 42.5 * i) | ||
palette[k++] = Math.floor(255 - 42.5 * j) | ||
palette[k++] = 0 | ||
palette[k++] = 255 | ||
} | ||
} | ||
for (i = 0; i < 6; i++) { | ||
for (j = 0; j < 6; j++) { | ||
var index = i * 6 + j + 1.5 // 0.5 to bias rounding | ||
var fraction = index / 255 | ||
ctx.fillStyle = 'rgba(0,0,0,' + fraction + ')' | ||
ctx.fillRect(j * 25, i * 25, 25, 25) | ||
} | ||
} | ||
|
||
canvas.createPNGStream({palette: palette}) | ||
.pipe(fs.createWriteStream(path.join(__dirname, 'indexed2.png'))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var Canvas = require('..') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var canvas = new Canvas(200, 200) | ||
var ctx = canvas.getContext('2d', {pixelFormat: 'A8'}) | ||
|
||
// Matches the "fillStyle" browser test, made by manipulating imageData | ||
var palette = new Uint8ClampedArray(37 * 4) | ||
var k = 0 | ||
var i, j | ||
// First value is opaque white: | ||
palette[k++] = 255 | ||
palette[k++] = 255 | ||
palette[k++] = 255 | ||
palette[k++] = 255 | ||
for (i = 0; i < 6; i++) { | ||
for (j = 0; j < 6; j++) { | ||
palette[k++] = Math.floor(255 - 42.5 * i) | ||
palette[k++] = Math.floor(255 - 42.5 * j) | ||
palette[k++] = 0 | ||
palette[k++] = 255 | ||
} | ||
} | ||
var idata = ctx.getImageData(0, 0, 200, 200) | ||
for (i = 0; i < 6; i++) { | ||
for (j = 0; j < 6; j++) { | ||
var index = j * 6 + i | ||
// fill rect: | ||
for (var xr = j * 25; xr < j * 25 + 25; xr++) { | ||
for (var yr = i * 25; yr < i * 25 + 25; yr++) { | ||
idata.data[xr * 200 + yr] = index + 1 | ||
} | ||
} | ||
} | ||
} | ||
ctx.putImageData(idata, 0, 0) | ||
|
||
canvas.createPNGStream({palette: palette}) | ||
.pipe(fs.createWriteStream(path.join(__dirname, 'indexed.png'))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.