Picture of Chris Gillison
Draggable image in graphic component
by Chris Gillison - Thursday, 3 February 2022, 11:15 AM
 

Hello everyone.

I'm trying to get the adapt-contrib-graphic to behave  little differently for a project I'm working. A large image needs to retain its actual width & height so it overflows the container and scrolls (rather than resizing to fit the container).

I've managed that bit, so I have scrollbars left & bottom of the container but really being able to grab and drag the image would be the smartest solution. Any ideas?

 

I poached (and tweaked) the following code from codepen and added it to the GraphicView.js, which summarily broke everything!!

const slider = this.model.getElementById ('.graphic__image-container');
let isDown = false;
let startX;
let scrollLeft;

slider.addEventListener('mousedown', (e) => {
  isDown = true;
  slider.classList.add('active');
  startX = e.pageX - slider.offsetLeft;
  scrollLeft = slider.scrollLeft;
});
slider.addEventListener('mouseleave', () => {
  isDown = false;
  slider.classList.remove('active');
});
slider.addEventListener('mouseup', () => {
  isDown = false;
  slider.classList.remove('active');
});
slider.addEventListener('mousemove', (e) => {
  if(!isDown) return;
  e.preventDefault();
  const x = e.pageX - slider.offsetLeft;
  const walk = (x - startX) * 3; //scroll-fast
  slider.scrollLeft = scrollLeft - walk;
  console.log(walk);
});

Picture of Oliver Foster
Re: Draggable image in graphic component
by Oliver Foster - Thursday, 3 February 2022, 2:50 PM
 

Hi Chris 

We have this oustanding issue which is waiting for resource: https://github.com/adaptlearning/adapt_framework/issues/3091

It reasonably describes your feature request, minus the dragging.

Dragging is a somewhat difficult thing to achieve alongside scrolling but you're effectively:

  • Taking the difference between (the delta of) the current coordinates of the document mouse move x, y and starting coordinates of the mouse down x, y and adding the horizontal and vertical  deltas to the scrollLeft and scrollTop of the image's overflow:scroll container accordingly.
  • You'll probably want a multiplier on the deltas so that you don't drag at 1:1 speed. This will be especially important with the different screen sizes from mobile to desktop, as a 1:1 drag will be very tedious in some large images.
  • You'll want to stop processing on the mouse up.
  • You want to measure the mouse delta over the screen / document rather than to any local position of the image.
  • At best case, you'd also make it keyboard accessible but that's why we've stalled as it's quite a niche thing to implement.

I hope that helps?

Ollie

Picture of Paul Steven
Re: Draggable image in graphic component
by Paul Steven - Thursday, 3 February 2022, 3:57 PM
 

Oops - think I was going to pick up that scrollable graphic. I had originally forked the graphics component to do this thinking it wasn't allowed in the core graphics component. I did have a CSS solution but it was not controllable by the keyboard so I had to create a component. I got this working with the keyboard - let me dig it out and do a PR and take it from there.

Note I do not have dragging. I did create an advanced zoom recently for our other elearning authoring tool that has advanced functionality such as scaling, changing contrast and dragging. I used Fabric.js for this however and it uses a Canvas element. I was thinking this functionality would be a great addition to Adapt but there may be a better library to use than Fabric as it is fairly long in the tooth.

Paul

Picture of Chris Gillison
Re: Draggable image in graphic component
by Chris Gillison - Thursday, 3 February 2022, 6:17 PM
 

Thanks Paul. Think I'm gonna leave it as a scrollable thing for now. This CSS seemed to do the trick (although I'll probably do something about that specific max-height):

.scroll-image {
    .graphic__widget {
        overflow: scroll;
        max-height: 505px;
    }
    img {
        max-width: none;
        min-width: none;
    }
}
Picture of Paul Steven
Re: Draggable image in graphic component
by Paul Steven - Thursday, 3 February 2022, 7:57 PM
 

Hey Chris

This is the CSS I used but as mentioned this isn't accessible with the keyboard. I just tried my forked graphic component solution in my latest Adapt Builder and it seems broken. So I need to fix this:) It was a good 9 months ago I created it and haven't tested it since. Note my CSS solution is horizontal scrolling only as that is all that was required.

.graphic__image-container {
height: 100%;
overflow: auto;
overflow-y: hidden;
white-space: nowrap;
}

.graphic__image-container img {
width: unset;
}

.graphic__image-container img, object, embed {
max-width: unset;
}

.graphic__image-container img:not([width]) {
width: unset;
}

 

 

Picture of Chris Gillison
Re: Draggable image in graphic component
by Chris Gillison - Thursday, 3 February 2022, 6:14 PM
 

SHEEESH! Maybe the dragging bit will remain a nice-to-have for the time being. Thanks Ollie