Skip to content

Commit 2f84eee

Browse files
Tirondzozbjornson
authored andcommittedApr 20, 2021
Add ctx2d.getTransform() function
Based on currentTransform getter
1 parent d107c04 commit 2f84eee

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed
 

‎src/CanvasRenderingContext2d.cc

+25-4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Context2d::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
123123
Nan::SetPrototypeMethod(ctor, "rotate", Rotate);
124124
Nan::SetPrototypeMethod(ctor, "translate", Translate);
125125
Nan::SetPrototypeMethod(ctor, "transform", Transform);
126+
Nan::SetPrototypeMethod(ctor, "getTransform", GetTransform);
126127
Nan::SetPrototypeMethod(ctor, "resetTransform", ResetTransform);
127128
Nan::SetPrototypeMethod(ctor, "setTransform", SetTransform);
128129
Nan::SetPrototypeMethod(ctor, "isPointInPath", IsPointInPath);
@@ -1751,11 +1752,11 @@ NAN_SETTER(Context2d::SetQuality) {
17511752
}
17521753

17531754
/*
1754-
* Get current transform.
1755+
* Helper for get current transform matrix
17551756
*/
17561757

1757-
NAN_GETTER(Context2d::GetCurrentTransform) {
1758-
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
1758+
Local<Object>
1759+
get_current_transform(Context2d *context) {
17591760
Isolate *iso = Isolate::GetCurrent();
17601761

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

17721773
const int argc = 1;
17731774
Local<Value> argv[argc] = { arr };
1774-
Local<Object> instance = Nan::NewInstance(_DOMMatrix.Get(iso), argc, argv).ToLocalChecked();
1775+
return Nan::NewInstance(context->_DOMMatrix.Get(iso), argc, argv).ToLocalChecked();
1776+
}
1777+
1778+
/*
1779+
* Get current transform.
1780+
*/
1781+
1782+
NAN_GETTER(Context2d::GetCurrentTransform) {
1783+
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
1784+
Local<Object> instance = get_current_transform(context);
17751785

17761786
info.GetReturnValue().Set(instance);
17771787
}
@@ -2243,6 +2253,17 @@ NAN_METHOD(Context2d::Transform) {
22432253
cairo_transform(context->context(), &matrix);
22442254
}
22452255

2256+
/*
2257+
* Get the CTM
2258+
*/
2259+
2260+
NAN_METHOD(Context2d::GetTransform) {
2261+
Context2d *context = Nan::ObjectWrap::Unwrap<Context2d>(info.This());
2262+
Local<Object> instance = get_current_transform(context);
2263+
2264+
info.GetReturnValue().Set(instance);
2265+
}
2266+
22462267
/*
22472268
* Reset the CTM, used internally by setTransform().
22482269
*/

‎src/CanvasRenderingContext2d.h

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Context2d: public Nan::ObjectWrap {
7676
static NAN_METHOD(Translate);
7777
static NAN_METHOD(Scale);
7878
static NAN_METHOD(Transform);
79+
static NAN_METHOD(GetTransform);
7980
static NAN_METHOD(ResetTransform);
8081
static NAN_METHOD(SetTransform);
8182
static NAN_METHOD(IsPointInPath);

‎test/canvas.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,8 @@ describe('Canvas', function () {
991991
var mat3 = ctx.currentTransform;
992992
assert.equal(mat3.a, 0.1);
993993
assert.equal(mat3.d, 0.3);
994+
995+
assert.deepEqual(ctx.currentTransform, ctx.getTransform());
994996
});
995997

996998
it('Context2d#createImageData(ImageData)', function () {

0 commit comments

Comments
 (0)
Please sign in to comment.