I keep seeing questions about dragging an element onto another element to do things like change scene.
Here is a quick and simple example to show how you can achieve this.
The original code comes from a Tumult Tutorial about dragging and controlling timelines which I always struggle to find.
But have the link at home. @Daniel I think it was one of yours ( warmer - colder )
Updated with these images:
You have to drag into the borders of a button. The margin for detection is small. Which can be adjusted in the code as needed
( Done in hype 2.5 )
( I will probably improve the example later as I am constrained for time right now and this is taken from some of my existing pages. )
I’m not sure if I get this right. I opened the scene in H3 pro and preview it in Safari. But there is no action when I drag the bubbles? Where should the bubbles be dropped? Or is it only working in H2.5?
I used your code for a turning dial knob. On every 45 degrees I want it to stop and show a button. So it’s kind of a circulair menu. I already played with a Greensocks jQuery option, but it didn’t bring me where I want to be.Lack of coding experience I guess
So with your code I made a new version. i works kind of, but not completely like it should. On turning the dial I switch to a new scene where the button shows up. I use invisible dropzones. From that point it should turn to the next stop (scene) with another button and so forth.
Something is not working good yet. Any clues?
I attach my testscene on this.
Thanks for the suggestion, but I’m afraid that example won’t help me with the actions I want to use on the 45 degree stops. I was hoping that by dragging the dial over certain dropzones this could be configured.
Anyway, thanks for your help and hopefully I didn’t hijack your thread too much…
Neat! Here’s another way to approach a basic drag and drop using MDN’s hittest formula, i kind of stopped using the built in draggable function since i found event.pageX ,Y A lot more basic than yours, but for basic drag and drop to do action ads it works lovely
Hi Luckyde,
I tried to use your JavaScript for my project but didn't work. I'm trying to put an element (a Boy) over another element (GreenRectangle) to start a timeline. Can you help me?
Note that the name of element is not the same as its Unique Element ID. These IDs are what link elements in your Hype document to something you can access in JavaScript.
Your untitledFunction() script references boy, Boy, and GreenRectangle. To get this to work you will need to:
Select the "Boy" element and go to the Identity Inspector. For the Unique Element ID, type in lowercase: boy.
Select the "GreenRectangle" element and type in the camel case GreenRectangle for the Unique Element ID.
In your code, change line 60 which references the uppercase Boy to the lowercase boy:
checker(Boy,GreenRectangle)
Becomes:
checker(boy,GreenRectangle)
Then the script will work!
Note that JavaScript automatically turns IDs into global variables, which is why this works. However valid names for IDs may not all be able to be represented by variable names. Therefore when working with code, it is recommended that you use a method to fetch the ID. In JavaScript this is typically document.getElementById() but Hype provides one that scopes more properly to the Hype animation on the page. Therefore at the top of the function you could use something like:
var boy = hypeDocument.getElementById("boy");
var GreenRectangle = hypeDocument.getElementById("GreenRectangle");
This isn't strictly necessary, but good programming practice to avoid using commonly named global variables that may conflict with other parts of the page and allow for better naming in JavaScript if you so choose.
At a glance the code itself is messing with CSS that Hype is probably also trying to alter in its own way by the flexible layout system.
I'd say the more compatible and current best practice way to do this is to use the Hype Drag and Drop Enabler created by @h_classen:
This is much simpler to use and does not require any code on your part. I tested with your simple grouping/flexible layout as shown in the video and it appeared to work just fine for me.
The steps to convert over to using this are:
Drag in the HypeDragAndDropEnabler.js file into the Resources Library and uncheck "Include in Document <head>"
Copy this code to the Head HTML (via the button in the Document Inspector):
For the Boy, change the On Drag action to simply be "Control Element Position"
In the Identity Inspector, add these Additional HTML Attributes to the Boy:
data-drag : true
data-drop : GreenRectangle
data-dropsnap : true (optional)
data-dragreverse : true (optional)
In the Identity Inspector, give the GreenRectangle a Class Name of GreenRectangle
In the Identity Inspector, add this Additional HTML Attributes to the GreenRectangle:
data-custombehavior : boylanded
In the Scene Inspector, click "Add New Behavior" to create a custom behavior. Give it the name from step 6 of "boylanded" and add an action to Start Timeline with your "Excellent" timeline