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

Add ctx.getTransform() and ctx.setTransform(DOMMatrix) methods #1769

Merged
merged 2 commits into from
Apr 20, 2021
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
74 changes: 60 additions & 14 deletions src/CanvasRenderingContext2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Context2d::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
Nan::SetPrototypeMethod(ctor, "rotate", Rotate);
Nan::SetPrototypeMethod(ctor, "translate", Translate);
Nan::SetPrototypeMethod(ctor, "transform", Transform);
Nan::SetPrototypeMethod(ctor, "getTransform", GetTransform);
Nan::SetPrototypeMethod(ctor, "resetTransform", ResetTransform);
Nan::SetPrototypeMethod(ctor, "setTransform", SetTransform);
Nan::SetPrototypeMethod(ctor, "isPointInPath", IsPointInPath);
Expand Down Expand Up @@ -1751,11 +1752,11 @@ NAN_SETTER(Context2d::SetQuality) {
}

/*
* Get current transform.
* Helper for get current transform matrix
*/

NAN_GETTER(Context2d::GetCurrentTransform) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
Local<Object>
get_current_transform(Context2d *context) {
Isolate *iso = Isolate::GetCurrent();

Local<Float64Array> arr = Float64Array::New(ArrayBuffer::New(iso, 48), 0, 6);
Expand All @@ -1771,7 +1772,32 @@ NAN_GETTER(Context2d::GetCurrentTransform) {

const int argc = 1;
Local<Value> argv[argc] = { arr };
Local<Object> instance = Nan::NewInstance(_DOMMatrix.Get(iso), argc, argv).ToLocalChecked();
return Nan::NewInstance(context->_DOMMatrix.Get(iso), argc, argv).ToLocalChecked();
}

/*
* Helper for get/set transform.
*/

void parse_matrix_from_object(cairo_matrix_t &matrix, Local<Object> mat) {
cairo_matrix_init(&matrix,
Nan::To<double>(Nan::Get(mat, Nan::New("a").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("b").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("c").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("d").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("e").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("f").ToLocalChecked()).ToLocalChecked()).FromMaybe(0)
);
}


/*
* Get current transform.
*/

NAN_GETTER(Context2d::GetCurrentTransform) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
Local<Object> instance = get_current_transform(context);

info.GetReturnValue().Set(instance);
}
Expand All @@ -1792,14 +1818,7 @@ NAN_SETTER(Context2d::SetCurrentTransform) {
#endif

cairo_matrix_t matrix;
cairo_matrix_init(&matrix,
Nan::To<double>(Nan::Get(mat, Nan::New("a").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("b").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("c").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("d").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("e").ToLocalChecked()).ToLocalChecked()).FromMaybe(0),
Nan::To<double>(Nan::Get(mat, Nan::New("f").ToLocalChecked()).ToLocalChecked()).FromMaybe(0)
);
parse_matrix_from_object(matrix, mat);

cairo_transform(context->context(), &matrix);
}
Expand Down Expand Up @@ -2243,6 +2262,17 @@ NAN_METHOD(Context2d::Transform) {
cairo_transform(context->context(), &matrix);
}

/*
* Get the CTM
*/

NAN_METHOD(Context2d::GetTransform) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
Local<Object> instance = get_current_transform(context);

info.GetReturnValue().Set(instance);
}

/*
* Reset the CTM, used internally by setTransform().
*/
Expand All @@ -2258,8 +2288,24 @@ NAN_METHOD(Context2d::ResetTransform) {

NAN_METHOD(Context2d::SetTransform) {
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
cairo_identity_matrix(context->context());
Context2d::Transform(info);
if (info.Length() == 1) {
Local<Object> mat = Nan::To<Object>(info[0]).ToLocalChecked();

#if NODE_MAJOR_VERSION >= 8
Local<Context> ctx = Nan::GetCurrentContext();
if (!mat->InstanceOf(ctx, _DOMMatrix.Get(Isolate::GetCurrent())).ToChecked()) {
return Nan::ThrowTypeError("Expected DOMMatrix");
}
#endif

cairo_matrix_t matrix;
parse_matrix_from_object(matrix, mat);

cairo_set_matrix(context->context(), &matrix);
} else {
cairo_identity_matrix(context->context());
Context2d::Transform(info);
}
}

/*
Expand Down
1 change: 1 addition & 0 deletions src/CanvasRenderingContext2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Context2d: public Nan::ObjectWrap {
static NAN_METHOD(Translate);
static NAN_METHOD(Scale);
static NAN_METHOD(Transform);
static NAN_METHOD(GetTransform);
static NAN_METHOD(ResetTransform);
static NAN_METHOD(SetTransform);
static NAN_METHOD(IsPointInPath);
Expand Down
8 changes: 8 additions & 0 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,14 @@ describe('Canvas', function () {
var mat3 = ctx.currentTransform;
assert.equal(mat3.a, 0.1);
assert.equal(mat3.d, 0.3);

assert.deepEqual(ctx.currentTransform, ctx.getTransform());

ctx.setTransform(ctx.getTransform());
assert.deepEqual(mat3, ctx.getTransform());

ctx.setTransform(mat3.a, mat3.b, mat3.c, mat3.d, mat3.e, mat3.f);
assert.deepEqual(mat3, ctx.getTransform());
});

it('Context2d#createImageData(ImageData)', function () {
Expand Down