Understanding / Improving element sizing #266
Replies: 2 comments 1 reply
-
This is what I would like to try: GoalInitiate the ARSource, the ARContext and the threeJS renderer with the same dimensions to avoid wasting resources on offscreen calculations and simplify sizing and positioning.
The sizing and positioning of the source element and the threejs canvas could then be done via CSS only: position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: cover; This would also free us from running everything in a fullscreen layout and would enable having an instance in any container element. Steps to achieve this1. Make ARSource aware of their source dimensionsCurrently 2. Refactor examples... to work like:
OptionalIntroduce an optional Questions@kalwalt @nicolocarpignoli @nickw1 (+ anyone who knows the inner working of this lib better than me 🙏) Do you think this could be a good idea or do you see any conceptual problems with how jsartoolkit works? Could initiating a portrait-oriented Thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
@hatsumatsu i merged the ES6 branch into dev. I thinking that we could remove the portrait and landscape mode in artoolkit5-js } else {
this.ctx.save();
if(this.orientation === 'portrait') {
this.ctx.translate(this.canvas.width, 0);
this.ctx.rotate(Math.PI / 2);
this.ctx.drawImage(sourceImage, 0, 0, this.canvas.height, this.canvas.width); // draw video
} else {
this.ctx.drawImage(sourceImage, 0, 0, this.canvas.width, this.canvas.height); // draw video
}
this.ctx.restore();
let imageData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);
data = imageData.data;
} and removing all the code containing canvas and HTMLElement inside artoolkit5-js. Passing only imageData to the |
Beta Was this translation helpful? Give feedback.
-
Trying to integrate ar.js in a project and following the efforts to create an es6 build of thee lib 🙏 I'm currently trying to understand how the sizing of the video source and the threejs renderer works.
I would love to help out fixing potential bugs or simplifying the current API methods but might need some intro into how the current system works.
Reading the code this is what I understand:
Despite
ARController
andARSource
supporting custom width/height configurations they seem to be always initiated with a size of 640×480px. On mobile devices in portrait orientation, requesting a MediaStream with these constraints results in a video source of 480×640px (see test chart below). The ARController however does not react to this but still runs in 640x480. Logging its properties also shows that it runs in "landscape" orientation.The threejs renderer –at least in some examples– also runs in 640×480 no matter the screen aspect ratio and the device orientation.
The webcam video is than scaled to fullscreen via
ARSource.onResizeElement()
AR.js/three.js/src/threex/threex-artoolkitsource.js
Line 306 in c6661e2
This looks good.
Next step, the threejs renderer is sized and positioned via
ARSource.copyElementSizeTo()
AR.js/three.js/src/threex/threex-artoolkitsource.js
Line 360 in c6661e2
This is where I got lost: Instead of just copying the style properties calculated in
ARSource.onResizeElement()
there is a special handling of portrait mode including some magic numbers:AR.js/three.js/src/threex/threex-artoolkitsource.js
Line 372 in c6661e2
This probably handles the missing sync between the video source and the ARController and threejs renderer size...
Given that some examples resize the threejs renderer based on the (assumed) video size and some based on the browser window it seems like pure magic to me that this works at all 🔮
Could anyone with a deeper knowledg of arjs let me know if I got this right or I am missing something.
Thanks a lot!
Tested on Pixel 5 & iPhone 6
Beta Was this translation helpful? Give feedback.
All reactions