d2d
Functions for working with Direct2D graphics.
Functions for working with Direct2D graphics.
Brush(type, param1, param2, param3, param4)
Creates a drawing brush of the specified type. The meaning of the brush's input parameters depends on its type.
For type == BrushType.Solid:
- param1: brush colour in ARGB
For type == BrushType.LinearGradient:
- param1: start point coords of linear gradient in form of Array(2) (for ex.: [0, 0])
- param2: end point coords of linear gradient in form of Array(2) (for ex.: [100, 0])
- param3: gradient stops specified as an array with alternating position and color values for each stop (for ex.: [0.0, 0xFF000000, 0.5, 0xFFFF0000, 1.0, 0xFFFFFFFF])
- param4: wrap mode responsible for how the gradient is repeated when drawing. See BrushWrapMode. Default is BrushWrapMode.Tile
For type == BrushType.RadialGradient:
- param1: center point coords of radial gradient in form of Array(2) (for ex.: [50, 50])
- param2: radius values for X and Y axes in form of Array(2) (for ex.: [50, 50])
- param3: gradient stops specified as an array with alternating position and color values for each stop (for ex.: [0.0, 0xFF000000, 0.5, 0xFFFF0000, 1.0, 0xFFFFFFFF])
- param4: wrap mode responsible for how the gradient is repeated when drawing. See BrushWrapMode. Default is BrushWrapMode.Tile
For type == BrushType.Bitmap:
- param1: D2DBitmap object used for drawing by brush
- param2: wrap mode responsible for how the image is repeated when drawing. See BrushWrapMode
| Name | Type | Description |
|---|---|---|
type | BrushType | |
param1 | * | |
param2 = undefinedoptional | * | |
param3 = undefinedoptional | * | |
param4 = undefinedoptional | * |
D2DBrush — Brush object used in Draw/Fill methods
Compile(source, entryPoint, target, flags)
Compiles Direct2D shader.
| Name | Type | Description |
|---|---|---|
source | string | Shader source code (ASCII HLSL code) |
entryPoint = "main"optional | string | The name of the shader entry point function where shader execution begins. |
target = ""optional | string | A string that specifies the shader target or set of shader features to compile against. |
flags = 0x4A008optional | D2DCompileFlags | Affects compiler flags. D2DCompileFlags |
D2DCompileInfo — Result of compiling shader source
include(`${fb.ComponentPath}\\docs\\Effects.js`);
window.DrawMode = 1;
// Simple colour inversion shader
const shaderSource = `
// Input texture (Direct2D passed it into t0)
Texture2D InputTexture : register(t0);
SamplerState InputSampler : register(s0);
// Direct2D input data structure
struct VS_OUTPUT {
float4 clipSpacePos : SV_POSITION;
float4 sceneSpacePos : SCENE_POS;
float4 texelSpacePos : TEXEL_POS;
};
// Simple color inversion shader
float4 main(VS_OUTPUT input) : SV_Target
{
// Selecting a pixel from an input image
float4 color = InputTexture.Sample(InputSampler, input.texelSpacePos.xy);
// Invert RGB, keep alpha
return float4(1.0 - color.rgb, color.a) * color.a;
}
`;
const img = d2d.Image(`${fb.ComponentPath}\\samples\\d2d\\images\\Flowers.jpg`);
const effect = d2d.Effect(Effects.CustomShader.ID);
effect.SetInput(0, img);
const shaderCode = d2d.Compile(shaderSource);
if (shaderCode.Error !== "")
fb.ShowPopupMessage(shaderCode.Error, "Direct2D compile error!");
else
effect.SetValue(Effects.CustomShader.ShaderCode, shaderCode.Code);
function on_paint(dgr) {
dgr.DrawEffect(effect, 10, 10, 0, 0, img.Width, img.Height);
}
CreateImage(w, h)
| Name | Type | Description |
|---|---|---|
w | number | |
h | number |
CreateImageFromPixelData(pixelData, width, height, format = "bgra32")
Create D2DBitmap from raw pixel data in memory.
| Name | Type | Description |
|---|---|---|
pixelData | Uint8Array | Raw pixel bytes |
width | number | Image width in pixels |
height | number | Image height in pixels |
format = "bgra32"optional | string | Pixel format string (default: "bgra32") Supported formats: |
D2DBitmap — null if was an error (for example pixelData array length is not suitable for the specified parameters)
Effect(CLSID)
Creates Direct2D effect.
Minimum system requirements: Windows 7 Platform Update (Direct2D 1.1)
| Name | Type | Description |
|---|---|---|
CLSID | string | CLSID of Direct2D effect. See Effects for effects' CLSID. |
Font(name, size_px, style)
Performance note: avoid using inside on_paint.
Performance note II: try caching and reusing D2DFont objects, since the maximum amount of such objects is hard-limited by Windows. D2DFont creation will fail after reaching this limit.
| Name | Type | Description |
|---|---|---|
name | string | |
size_px | number | See Point2Pixel function for conversions |
style = 0optional | number | See FontStyle flags |
?D2DFont — null, if font is not present.
Image(path)
Load image from file.
Performance note: consider using d2d.LoadImageAsync or d2d.LoadImageAsyncV2 if there are a lot of images to load or if the image is big.
| Name | Type | Description |
|---|---|---|
path | string |
?D2DBitmap — null, if image failed to load.
let img = d2d.Image('e:\\images folder\\my_image.png');
LoadImageAsync(window_id, path)
Load image from file asynchronously.
| Name | Type | Description |
|---|---|---|
window_id | number | unused |
path | string |
number — a unique id, which is used in on_load_image_done.
LoadImageAsyncV2(window_id, path)
Load image from file asynchronously. Returns a Promise object, which will be resolved when image loading is done.
| Name | Type | Description |
|---|---|---|
window_id | number | unused |
path | string |
Promise.<?D2DBitmap>LoadSVG(path_or_xml, max_width)
Loads rasterized image from SVG file or XML string
| Name | Type | Description |
|---|---|---|
path_or_xml | string | string containing SVG file path or raw XML |
max_width = 0optional | number | If specified rasterizes with width = max_width and height according to the proportions, otherwise uses "width" and "height" attributes in SVG header if exist |
?D2DBitmap — Rasterized bitmap, null in case of error
const svg_file = fb.ComponentPath + 'samples\\svg\\android.svg';
const original = d2d.LoadSVG(svg_file);
const large = d2d.LoadSVG(svg_file, 512); // set optional max_width
function on_paint(gr) {
gr.DrawImage(original, 0, 0, original.Width, original.Height, 0, 0, original.Width, original.Height);
gr.DrawImage(large, original.Width, 0, large.Width, large.Height, 0, 0, large.Width, large.Height);
}