Definitely possible.
Here's a port of the tk2dUIDragItem class to JS to get you started.
* Remember to run SetupForJS, otherwise the classes won't be accessible from JS.
* Also note - apart from the usual suspects, variable & function definition syntax, there isn't REALLY any difference there.
#pragma strict
class DragItem extends tk2dUIBaseItemControl {
private var offset : Vector3 = Vector3.zero; //offset on touch/click
private var isBtnActive : boolean = false; //if currently active
function OnEnable()
{
if (uiItem != null)
{
uiItem.OnDown += ButtonDown;
uiItem.OnRelease += ButtonRelease;
}
}
function OnDisable()
{
if (uiItem != null)
{
uiItem.OnDown -= ButtonDown;
uiItem.OnRelease -= ButtonRelease;
}
if (isBtnActive)
{
if (tk2dUIManager.Instance != null)
{
tk2dUIManager.Instance.OnInputUpdate -= UpdateBtnPosition;
}
isBtnActive = false;
}
}
private function UpdateBtnPosition()
{
transform.position = CalculateNewPos();
}
private function CalculateNewPos() : Vector3
{
var pos : Vector2 = uiItem.Touch.position;
var worldPos : Vector3 = tk2dUIManager.Instance.UICamera.ScreenToWorldPoint(new Vector3(pos.x, pos.y, transform.position.z - tk2dUIManager.Instance.UICamera.transform.position.z));
worldPos.z = transform.position.z;
worldPos += offset;
return worldPos;
}
/// <summary>
/// Set button to be down (drag can begin)
/// </summary>
public function ButtonDown()
{
if (!isBtnActive)
{
tk2dUIManager.Instance.OnInputUpdate += UpdateBtnPosition;
}
isBtnActive = true;
offset = Vector3.zero;
var newWorldPos = CalculateNewPos();
offset = transform.position - newWorldPos;
}
/// <summary>
/// Set button release (so drag will stop)
/// </summary>
public function ButtonRelease()
{
if (isBtnActive)
{
tk2dUIManager.Instance.OnInputUpdate -= UpdateBtnPosition;
}
isBtnActive = false;
}
}