Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix change size of canvas element. #276

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 23 additions & 1 deletion integration_tests/specs/dom/elements/canvas/context2d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
describe('Canvas context 2d', () => {
it('can change size by width and height property', async () => {
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);

var context = canvas.getContext('2d');
if (!context) {
throw new Error('canvas context is null');
}

canvas.width = canvas.height = 300;
// Scaled rectangle
context.fillStyle = "red";
context.fillRect(10, 10, 380, 380);

await snapshot();
canvas.width = canvas.height = 400;
// Scaled rectangle
context.fillStyle = "red";
context.fillRect(10, 10, 380, 380);
await snapshot();
});

it('should work with font and rect', async () => {
var div = document.createElement('div');
div.style.width = div.style.height = '300px';
Expand Down Expand Up @@ -426,7 +448,7 @@ describe('Canvas context 2d', () => {

await snapshot();
});

it('should work with createLinearGradient', async (done) => {
const canvas = <canvas height="300" width="300" />;
document.body.appendChild(canvas);
Expand Down
2 changes: 1 addition & 1 deletion webf/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Demonstrates how to use the webf plugin.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"

dependencies:
flutter:
Expand Down
1 change: 1 addition & 0 deletions webf/lib/src/bridge/native_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ void toNativeValue(Pointer<NativeValue> target, value, [BindingObject? ownerBind
target.ref.uint32 = JSPointerType.Others.index;
target.ref.u = value.address;
} else if (value is BindingObject) {
assert((value.pointer)!.address != nullptr);
target.ref.tag = JSValueType.TAG_POINTER.index;
target.ref.uint32 = JSPointerType.NativeBindingObject.index;
target.ref.u = (value.pointer)!.address;
Expand Down
10 changes: 6 additions & 4 deletions webf/lib/src/html/canvas/canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ class CanvasElement extends Element {
// When the user agent is to set bitmap dimensions to width and height, it must run these steps:
// 1. Reset the rendering context to its default state.
context2d?.reset();
// 2. Resize the output bitmap to the new width and height and clear it to transparent black.
resize();
// 3. Let canvas be the canvas element to which the rendering context's canvas attribute was initialized.
// 4. If the numeric value of canvas's width content attribute differs from width,

// 2. Let canvas be the canvas element to which the rendering context's canvas attribute was initialized.
// 3. If the numeric value of canvas's width content attribute differs from width,
// then set canvas's width content attribute to the shortest possible string representing width as
// a valid non-negative integer.
if (width != null && width.toString() != getAttribute(WIDTH)) {
Expand All @@ -251,6 +250,9 @@ class CanvasElement extends Element {
if (height < 0) height = 0;
internalSetAttribute(HEIGHT, height.toString());
}

// 4. Resize the output bitmap to the new width and height and clear it to transparent black.
resize();
}

void _styleChangedListener(String key, String? original, String present) {
Expand Down