Skip to content

Commit

Permalink
Add ctx2d.getTransform() function
Browse files Browse the repository at this point in the history
Based on currentTransform getter
  • Loading branch information
Tirondzo authored and zbjornson committed Apr 20, 2021
1 parent d107c04 commit 2f84eee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/CanvasRenderingContext2d.cc
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,16 @@ 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();
}

/*
* 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 Down Expand Up @@ -2243,6 +2253,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 Down
1 change: 1 addition & 0 deletions src/CanvasRenderingContext2d.h
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
2 changes: 2 additions & 0 deletions test/canvas.test.js
Expand Up @@ -991,6 +991,8 @@ 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());
});

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

0 comments on commit 2f84eee

Please sign in to comment.