Because premultiplied 8-bit color isn't enough! This project provides a "hdr2d" canvas render context—which internally represents colors using 32-bit float per channel. Furthermore, it provides a variety of features useful for art that the traditional "2d" context does not: blending modes, levels, and more.
var canvas = document.createElement('canvas');
canvas.width = 100;
canvas.height = 100;
document.body.appendChild(canvas);
var context = canvas.getContext('hdr2d');
// draw like you normally would with canvas!
To activate a particular mode, set the context's globalBlendMode
property:
context.globalBlendMode = HDR2D_BLEND_ADD;
Descriptions from SVG Compositing Specification.
Channel levels can be adjusted individually through the range
property. Because colors aren't clamped like they are with "2d", the levels don't necessarily need to be 0–255. Note: modifying the levels will not modify your pixel data; it will only affect how it is rendered.
context.range = {
r: {low: 0, high: 255},
g: {low: 0, high: 255},
b: {low: 0, high: 255},
a: {low: 0, high: 255}
};
Once you have modified the levels, refresh the display:
context.invalidate();
Unlike the traditional "2d" context, "hdr2d" exposes the raw pixel buffer as context.imageData
(no need to call getImageData()
). After you have modified the buffer, make sure to call context.invalidate()
to refresh the canvas.
An object containing output display levels (by channel). See Levels.
A 0.0–1.0 alpha channel multiplier applied to drawing operations.
The blending mode used in drawing operations. See Blending Modes.
The color used by fillRect()
. Valid formats: #000000
, #000
(shorthand), rgba(0,0,0,0.5)
, rgba(0,0,0)
, hsl(20,100%,100%)
, hsla(20, 100%,100%,0.5)
. See [fillStyle] for more information.
Returns an object containing r
, g
, b
, and a
color components.
Composites the color at (x, y). The color
argument should be an object containing r
, g
, b
, and a
color components.
Draws a rectangle at (x, y). To set the color, set fillStyle.
Resets the pixels in the given region to (0, 0, 0, 0).
Composites the image at (x, y). This method complies with the drawImage() specification.
Returns the raw image data for the given region (it will be an ImageDataHDR
object, which mirrors ImageData in design).
Re-renders a region of the canvas. If no arguments are given, the entire canvas is invalidated.
Copyright © 2012 — Brian Reavis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.