User:Monster Domosed/common.js: Difference between revisions

Undo
Tag: Undo
No edit summary
Line 30: Line 30:
             startFrame  = +el.dataset.startFrame  || 0,
             startFrame  = +el.dataset.startFrame  || 0,
             sensitivity = +el.dataset.sensitivity || 5;
             sensitivity = +el.dataset.sensitivity || 5;
        /* ---- 1:1 square for 3D strip ---- */
        var sq = Math.min(fw, fh);


         var hint = el.querySelector('.rotatable-model-hint'),
         var hint = el.querySelector('.rotatable-model-hint'),
Line 44: Line 47:
             is2D  = false,
             is2D  = false,
             dragOK = true,
             dragOK = true,
             ready  = false;   // true once the strip image has loaded
             ready  = false;


         /* ============================================================
         /* ============================================================
           3D SPRITE STRIP
           3D SPRITE STRIP — each frame is sq × sq
           ============================================================ */
           ============================================================ */
         var img = makeImg(imageUrl, '', 'rm-strip');
         var img = makeImg(imageUrl, '', 'rm-strip');


        // Set explicit display dimensions
         img.style.height = sq + 'px';
        // width = single frame width × number of frames
         img.style.width  = (sq * count) + 'px';
        // height = frame height
         img.style.height = fh + 'px';
         img.style.width  = (fw * count) + 'px';


         img.addEventListener('load', function () {
         img.addEventListener('load', function () {
Line 68: Line 68:
         el.insertBefore(img, el.firstChild);
         el.insertBefore(img, el.firstChild);


        // Handle cached images
         if (img.complete && img.naturalWidth > 0) {
         if (img.complete && img.naturalWidth > 0) {
             ready = true;
             ready = true;
Line 76: Line 75:


         /* ============================================================
         /* ============================================================
           2D STATIC IMAGE
           2D STATIC IMAGE — renders at full fw × fh
           ============================================================ */
           ============================================================ */
         var staticImg = null;
         var staticImg = null;
Line 127: Line 126:


         /* ============================================================
         /* ============================================================
           FRAME DISPLAY
           FRAME DISPLAY — sq × sq centred in fw × fh viewport
           ============================================================ */
           ============================================================ */
         function show(idx) {
         function show(idx) {
             cur = ((idx % count) + count) % count;
             cur = ((idx % count) + count) % count;
            // Move the strip so that frame `cur` is visible in the viewport
             img.style.left = ((fw - sq) / 2 - cur * sq) + 'px';
            var offset = cur * fw;
             img.style.top  = ((fh - sq) / 2) + 'px';
             img.style.left = '-' + offset + 'px';
             img.style.top  = '0px';
         }
         }


Line 187: Line 184:
         /* ============================================================
         /* ============================================================
           DRAG  (mouse + touch)
           DRAG  (mouse + touch)
          One frame per `sensitivity` pixels of movement.
          Clamped to ±1 frame per event to prevent skipping.
           ============================================================ */
           ============================================================ */
         function startDrag(x0) {
         function startDrag(x0) {
Line 200: Line 195:
                 acc += dx;
                 acc += dx;


                // Convert accumulated pixels to frame steps
                 while (acc >= sensitivity) {
                 while (acc >= sensitivity) {
                     acc -= sensitivity;
                     acc -= sensitivity;
Line 243: Line 237:
         }, { passive: false });
         }, { passive: false });


         /* ---- wheel: always ±1 frame per tick ---- */
         /* ---- wheel ---- */
         el.addEventListener('wheel', function (e) {
         el.addEventListener('wheel', function (e) {
             if (!dragOK || !ready) return;
             if (!dragOK || !ready) return;