@@ -55,6 +55,58 @@ export class NSCMTLView extends NSView {
55
55
static {
56
56
NativeClass ( this ) ;
57
57
}
58
+ _device ;
59
+ _queue ;
60
+ _canvas ;
61
+
62
+ get queue ( ) {
63
+ return this . _queue ;
64
+ }
65
+
66
+ get device ( ) {
67
+ return this . _device ;
68
+ }
69
+
70
+ initWithFrame ( frameRect ) {
71
+ super . initWithFrame ( frameRect ) ;
72
+ this . wantsLayer = true ;
73
+ const layer = CAMetalLayer . layer ( ) ;
74
+ this . _device = MTLCreateSystemDefaultDevice ( ) ;
75
+ this . _queue = this . _device . newCommandQueue ( ) ;
76
+ layer . device = this . _device ;
77
+ layer . presentsWithTransaction = false ;
78
+ layer . framebufferOnly = false ;
79
+ layer . pixelFormat = MTLPixelFormat . BGRA8Unorm ;
80
+
81
+ this . layer = layer ;
82
+ return this ;
83
+ }
84
+
85
+
86
+ /**
87
+ * @return {CGSize }
88
+ */
89
+ get drawableSize ( ) {
90
+ return this . layer . drawableSize ;
91
+ }
92
+
93
+ /**
94
+ * @param {CGSize } value
95
+ */
96
+ set drawableSize ( value ) {
97
+ this . layer . drawableSize = value ;
98
+ }
99
+
100
+
101
+ present ( ) {
102
+ this . _canvas ?. getContext ( '2d' ) . flush ( ) ;
103
+ this . _canvas ?. getContext ( '2d' ) . present ( ) ;
104
+ }
105
+
106
+ static ObjCExposedMethods = {
107
+ present : { returns : interop . types . void , params : [ ] }
108
+ } ;
109
+
58
110
}
59
111
60
112
export class NSCCanvas extends NSView {
@@ -89,6 +141,7 @@ export class CanvasGLView extends NSOpenGLView {
89
141
}
90
142
}
91
143
144
+
92
145
export class ViewController extends NSViewController {
93
146
static {
94
147
NativeClass ( this ) ;
@@ -105,15 +158,25 @@ export class ViewController extends NSViewController {
105
158
this . view . addSubview ( this . canvas ) ;
106
159
107
160
glview . frame = this . view . frame ;
161
+ mtlview . frame = this . view . frame ;
162
+ mtlview . drawableSize = new CGSize ( {
163
+ width : this . view . frame . size . width * NSScreen . mainScreen . backingScaleFactor ,
164
+ height : this . view . frame . size . height * NSScreen . mainScreen . backingScaleFactor
165
+ } ) ;
166
+
167
+ this . canvas . addSubview ( mtlview ) ;
108
168
109
- this . canvas . addSubview ( glview ) ;
169
+ // this.canvas.addSubview(glview);
110
170
111
- glview . wantsLayer = true ;
171
+ // glview.wantsLayer = true;
112
172
}
113
173
}
114
174
115
175
const glview = CanvasGLView . alloc ( ) . initWithFrame ( { x : 0 , y : 0 , width : 0 , height : 0 } ) ;
116
176
177
+ const mtlview = NSCMTLView . alloc ( ) . initWithFrame ( { x : 0 , y : 0 , width : 0 , height : 0 } ) ;
178
+
179
+
117
180
let isDoingOrDone = false ;
118
181
119
182
function mdnShadowColor ( ctx ) {
@@ -399,8 +462,8 @@ function flappyBird(canvas) {
399
462
}
400
463
401
464
function main ( ) {
402
- width = canvas . clientWidth * window . devicePixelRatio ;
403
- height = canvas . clientHeight * window . devicePixelRatio ;
465
+ width = canvas . clientWidth // * window.devicePixelRatio;
466
+ height = canvas . clientHeight // * window.devicePixelRatio;
404
467
405
468
canvas . addEventListener ( 'touchstart' , onpress ) ;
406
469
@@ -981,6 +1044,47 @@ function mdnPattern(canvas) {
981
1044
ctx . fillRect ( 0 , 0 , canvas . width , canvas . height ) ;
982
1045
}
983
1046
1047
+ function mdnPutImageData ( canvas ) {
1048
+ const ctx = canvas . getContext ( '2d' ) ;
1049
+
1050
+ function putImageData (
1051
+ ctx ,
1052
+ imageData ,
1053
+ dx ,
1054
+ dy ,
1055
+ dirtyX ,
1056
+ dirtyY ,
1057
+ dirtyWidth ,
1058
+ dirtyHeight
1059
+ ) {
1060
+ const data = imageData . data ;
1061
+ const height = imageData . height ;
1062
+ const width = imageData . width ;
1063
+ dirtyX = dirtyX || 0 ;
1064
+ dirtyY = dirtyY || 0 ;
1065
+ dirtyWidth = dirtyWidth !== undefined ? dirtyWidth : width ;
1066
+ dirtyHeight = dirtyHeight !== undefined ? dirtyHeight : height ;
1067
+ const limitBottom = dirtyY + dirtyHeight ;
1068
+ const limitRight = dirtyX + dirtyWidth ;
1069
+ for ( let y = dirtyY ; y < limitBottom ; y ++ ) {
1070
+ for ( let x = dirtyX ; x < limitRight ; x ++ ) {
1071
+ const pos = y * width + x ;
1072
+ ctx . fillStyle = `rgb(${ data [ pos * 4 + 0 ] } ${ data [ pos * 4 + 1 ] }
1073
+ ${ data [ pos * 4 + 2 ] } / ${ data [ pos * 4 + 3 ] / 255 } )` ;
1074
+ ctx . fillRect ( x + dx , y + dy , 1 , 1 ) ;
1075
+ }
1076
+ }
1077
+ }
1078
+
1079
+ // Draw content onto the canvas
1080
+ ctx . fillRect ( 0 , 0 , 100 , 100 ) ;
1081
+ // Create an ImageData object from it
1082
+ const imagedata = ctx . getImageData ( 0 , 0 , 100 , 100 ) ;
1083
+ // use the putImageData function that illustrates how putImageData works
1084
+ putImageData ( ctx , imagedata , 150 , 0 , 50 , 50 , 25 , 25 ) ;
1085
+ ctx . render ( ) ;
1086
+ }
1087
+
984
1088
function mdnRadialGradient ( canvas ) {
985
1089
const ctx = canvas . getContext ( '2d' ) ;
986
1090
@@ -1028,7 +1132,31 @@ function doTheThing() {
1028
1132
// const glContext = WebGLRenderingContext.withView(handle.toNumber(), 1, true, false, false, false, 1, true, false, false, false, false, false);
1029
1133
// console.log(glContext, 'drawingBufferWidth', glContext.drawingBufferWidth, 'drawingBufferHeight', glContext.drawingBufferHeight);
1030
1134
1031
- const ctx = CanvasRenderingContext2D . withView ( handle . toNumber ( ) , glview . frame . size . width * scale , glview . frame . size . height * scale , 1 , true , 0 , 90 , 1 ) ;
1135
+ //const ctx = CanvasRenderingContext2D.withView(handle.toNumber(), glview.frame.size.width * scale, glview.frame.size.height * scale, 1, true, 0, 90, 1);
1136
+
1137
+
1138
+ const mtlViewHandle = interop . handleof ( mtlview ) ;
1139
+ const deviceHandle = interop . handleof ( mtlview . device ) ;
1140
+ const queueHandle = interop . handleof ( mtlview . queue ) ;
1141
+ const ctx = CanvasRenderingContext2D . withMtlViewDeviceQueue (
1142
+ mtlViewHandle . toNumber ( ) , deviceHandle . toNumber ( ) , queueHandle . toNumber ( ) , true , scale , 1 , 0 , 90 , 1
1143
+ ) ;
1144
+
1145
+ const mtlCanvas = {
1146
+ width : mtlview . frame . size . width ,
1147
+ height : mtlview . frame . size . height ,
1148
+ clientWidth : mtlview . frame . size . width ,
1149
+ clientHeight : mtlview . frame . size . height ,
1150
+ addEventListener : function ( ) {
1151
+ } ,
1152
+ getContext : function ( ) {
1153
+ return ctx ;
1154
+ } ,
1155
+ style : { }
1156
+ } ;
1157
+
1158
+ mtlview . _canvas = mtlCanvas ;
1159
+
1032
1160
ctx . fillStyle = 'white' ;
1033
1161
1034
1162
ctx . fillRect ( 0 , 0 , 1000 , 1000 ) ;
@@ -1056,16 +1184,22 @@ function doTheThing() {
1056
1184
style : { }
1057
1185
} ;
1058
1186
1059
- // flappyBird(canvas );
1187
+ // flappyBird(mtlCanvas );
1060
1188
1061
- solarSystem ( canvas ) ;
1189
+ solarSystem ( canvas ) ;
1062
1190
1063
1191
// swarm(canvas);
1064
- //breathe_demo(canvas);
1192
+ // breathe_demo(canvas);
1065
1193
//mdnPattern(canvas);
1066
- //mdnRadialGradient(canvas);
1194
+ // mdnRadialGradient(canvas);
1195
+ //mdnPutImageData(canvas);
1196
+
1197
+ //
1198
+ // ctx.font = '50px serif';
1199
+ // ctx.strokeText('Hello world', 50, 90);
1200
+
1067
1201
1068
- // ctx.render();
1202
+ // ctx.render();
1069
1203
1070
1204
/*
1071
1205
0 commit comments