Beispiel #1
0
def resize_handler():
    frame = document.getElementById('frame')
    canvas = document.getElementById('calendar_canvas')
    w = frame.scrollWidth
    # have to remember to account for
    # the padding, otherwise race condition
    # makes the canvas grow...
    canvas.width = (w - 32)
    canvas.height = (25 * 20)
    draw_calendar()
    print("resized")
    def __init__(self):
        self.keyCode = None
        self.pause = True

        self.canvasFrame = document.getElementById('canvas_frame')
        self.canvas = __new__(
            fabric.Canvas(
                'canvas', {
                    'backgroundColor': 'black',
                    'originX': 'center',
                    'originY': 'center'
                }))
        self.canvas.onWindowDraw = self.draw  # Install draw callback, will be called asynch
        self.canvas.lineWidth = 2
        self.canvas.clear()

        set_size = 6

        self.attributes = []
        squares = [Square(self, index) for index in range(set_size)]
        self.squares = [Square.set(square) for square in squares]

        window.setInterval(self.update,
                           10)  # Install update callback, time in ms
        window.setInterval(self.draw, 20)  # Install draw callback, time in ms
        window.addEventListener('keydown', self.keydown)
        window.addEventListener('keyup', self.keyup)

        self.buttons = []

        for key in ('F', 'J', 'space'):
            button = document.getElementById(key)
            button.addEventListener(
                'mousedown',
                (lambda aKey: lambda: self.mouseOrTouch(aKey, True)
                 )(key))  # Returns inner lambda
            button.addEventListener(
                'touchstart',
                (lambda aKey: lambda: self.mouseOrTouch(aKey, True))(key))
            button.addEventListener(
                'mouseup',
                (lambda aKey: lambda: self.mouseOrTouch(aKey, False))(key))
            button.addEventListener(
                'touchend',
                (lambda aKey: lambda: self.mouseOrTouch(aKey, False))(key))
            button.style.cursor = 'pointer'
            button.style.userSelect = 'none'
            self.buttons.append(button)

        self.time = +__new__(Date)
Beispiel #3
0
def delete_button_handler(username):
    def delete_user():
        nonlocal username
        if window.confirm("Really delete {}?".format(username)):
            window.location.href = "/delete/" + username

    document.getElementById('deleteBtn').onclick = delete_user
Beispiel #4
0
    def __init__ (self):
        self.pause = True                           # Start experiment in paused state
        self.keyCode = None
        
        self.textFrame = document.getElementById ('text_frame')
        self.canvasFrame = document.getElementById ('canvas_frame')
        self.buttonsFrame = document.getElementById ('buttons_frame')
        
        self.canvas = __new__ (fabric.Canvas ('canvas', {'backgroundColor': 'grey', 'originX': 'center', 'originY': 'center'}))
        self.canvas.onWindowDraw = self.draw        # Install draw callback, will be called asynch
        self.canvas.lineWidth = 2
        self.canvas.clear ()    

        self.set_size = 6
        self.attributes = []                        # All attributes will insert themselves here
        self.stimuli = [Stimuli(self, index) for index in range (self.set_size)]    # Pass game as parameter self
        self.fixation_point = Fixation(self)
        
        window.setInterval (self.update, 1)    # Install update callback, time in ms
        window.setInterval (self.draw, 20)      # Install draw callback, time in ms
        window.addEventListener ('keydown', self.keydown)
        window.addEventListener ('keyup', self.keyup)
        
        self.buttons = []
        
        for key in ('F', 'J','space'):
            button = document.getElementById (key)
            button.addEventListener ('mousedown', (lambda aKey: lambda: self.mouseOrTouch (aKey, True)) (key))  # Returns inner lambda
            button.addEventListener ('touchstart', (lambda aKey: lambda: self.mouseOrTouch (aKey, True)) (key))
            button.addEventListener ('mouseup', (lambda aKey: lambda: self.mouseOrTouch (aKey, False)) (key))
            button.addEventListener ('touchend', (lambda aKey: lambda: self.mouseOrTouch (aKey, False)) (key))
            button.style.cursor = 'pointer'
            button.style.userSelect = 'none'
            self.buttons.append (button)
        
            
        self.time = + __new__ (Date)
        
        self.start_exp_timer = self.time
        self.target_presented = bool
        self.isi_presented = bool
        self.all_presented = bool
        self.trial_set = bool
        self.target_color = []
        
        window.onresize = self.resize
        self.resize ()
Beispiel #5
0
def getPyElementById(idstr: str) -> typing.Optional["base_element"]:
    """Return a python element of the document element... Return None iff none found
    """
    jsel_found = document.getElementById(idstr)
    if jsel_found is None:
        return None
    # now determine the scoself object if we have one, otherwise create a new python object for it.
    py_obj = _mm.get_self(jsel_found.getAttribute('scoself'))
    py_obj = py_obj or base_element(jsel_found, idstr)
    return py_obj
    def __init__ (self):
        self.serviceIndex = 1 if Math.random () > 0.5 else 0    # Index of player that has initial service
        self.pause = True                           # Start game in paused state
        self.keyCode = None
        
        self.textFrame = document.getElementById ('text_frame')
        self.canvasFrame = document.getElementById ('canvas_frame')
        self.buttonsFrame = document.getElementById ('buttons_frame')
        
        self.canvas = __new__ (fabric.Canvas ('canvas', {'backgroundColor': 'black', 'originX': 'center', 'originY': 'center'}))
        self.canvas.onWindowDraw = self.draw        # Install draw callback, will be called asynch
        self.canvas.lineWidth = 2
        self.canvas.clear ()    

        self.attributes = []                        # All attributes will insert themselves here
        self.paddles = [Paddle (self, index) for index in range (2)]    # Pass game as parameter self
        self.ball = Ball (self)
        self.scoreboard = Scoreboard (self)     

        window.setInterval (self.update, 10)    # Install update callback, time in ms
        window.setInterval (self.draw, 20)      # Install draw callback, time in ms
        window.addEventListener ('keydown', self.keydown)
        window.addEventListener ('keyup', self.keyup)
        
        self.buttons = []
        
        for key in ('A', 'Z', 'K', 'M', 'space', 'enter'):
            button = document.getElementById (key)
            button.addEventListener ('mousedown', (lambda aKey: lambda: self.mouseOrTouch (aKey, True)) (key))  # Returns inner lambda
            button.addEventListener ('touchstart', (lambda aKey: lambda: self.mouseOrTouch (aKey, True)) (key))
            button.addEventListener ('mouseup', (lambda aKey: lambda: self.mouseOrTouch (aKey, False)) (key))
            button.addEventListener ('touchend', (lambda aKey: lambda: self.mouseOrTouch (aKey, False)) (key))
            button.style.cursor = 'pointer'
            button.style.userSelect = 'none'
            self.buttons.append (button)
            
        self.time = + __new__ (Date)
        
        window.onresize = self.resize
        self.resize ()
Beispiel #7
0
def show_times():
    names = document.getElementById('names')
    while names.firstChild:
        names.removeChild(names.firstChild)

    status = get_status()
    for idx, u in enumerate(status.keys()):

        sp = document.createElement('a')
        sp.innerHTML = u
        sp.style.backgroundColor = USER_COLORS[idx]
        sp.className = 'btn'
        sp.href = "/user/" + u
        names.appendChild(sp)
Beispiel #8
0
def create_engine(canvas_element=DEFAULT_CANVAS):
    nonlocal _CANVAS
    nonlocal _ENGINE
    _CANVAS = document.getElementById(canvas_element)
    if not _CANVAS:
        raise ValueError("No html5 canvas named", canvas_element)
    _ENGINE = Engine(_CANVAS, True)
    window.addEventListener("resize", lambda: _ENGINE.resize())

    def render_loop():
        if _SCENE:
            _SCENE.render()

    _ENGINE.runRenderLoop(render_loop)
    return _ENGINE
Beispiel #9
0
def getPyElementByIdClass(idstr: str, classname,
                          attrdct: dict) -> typing.Optional["base_element"]:
    """Return a python element given the id string of an existing HTML element.

    Args:
       idstr: the idstr of the HTML element existing in the DOM.
       classname: the class of the instance to create
       attrdct: the attribute dict of the newly created instance.

    Returns:
       A new python class of class classname that wraps the HTML element.
       None if no existing js element with the idstr is found.
    Note:
       We do not check whether the existing HTML element is of the same
       type as the desired python object; i.e. there is nothing to stop
       a caller from creating an html.a object for a predefined HTML select object.
    """
    jsel_found = document.getElementById(idstr)
    if jsel_found is None:
        return None
    # now determine the scoself object if we have one, otherwise create a new python object for it.
    return getPyElementByJsEl(idstr, jsel_found, classname, attrdct)
Beispiel #10
0
def draw_calendar():

    canvas = document.getElementById("calendar_canvas")

    w = canvas.scrollWidth
    h = canvas.scrollHeight

    ctx = canvas.getContext('2d')

    # rect is a 4-tuple of 0-1 coords

    def draw_rect(rect, fillStyle):
        left = rect[0] * (w - 2 * MARGIN) + MARGIN
        right = rect[2] * (w - 2 * MARGIN) + MARGIN
        top = rect[1] * h
        bottom = rect[3] * h
        ctx.fillStyle = fillStyle
        ctx.fillRect(left, top, right - left, bottom - top)

    draw_rect((0, 0, 1, 1), BACKGROUND)

    # basic colors
    for day in range(7):
        lf = day / 7
        r = (day + 1) / 7
        for hour in range(1, 25):
            top = hour / 25
            bottom = (hour + 1) / 25
            draw_rect((lf, top, r, bottom), hour_color(hour, day))

    # hour labels
    ctx.font = HR_FONT
    ctx.fillStyle = HR_LABEL
    for i in range(1, 24):

        if i % 2 == 0:
            divider = ((i + 1) / 25) * h + HOUR_OFFSET
            hr = (i % 12) or "Noon"

            if i % 12 < 10 and hr != "Noon":
                hr = str(" {}").format(hr)
            if i > 12:
                hr += " p"
            elif i < 12:
                hr += " a"

            ctx.fillText(hr, 2, divider)

    # day labels
    if w < 512:
        names = 'M', 'T', 'W', 'Th', 'F', 'Sa', 'Su'
    else:
        names = 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'

    ctx.font = DAY_FONT
    ctx.fillStyle = DAY_LABEL

    for i, t in enumerate(names):
        lf = int(((i + .5) / 7) * (w - 2 * MARGIN) + MARGIN)
        size = int(ctx.measureText(t).width / 2)
        ctx.fillText(t, lf - size, 18, int(w / 7))

    status = dict(get_status())

    user_count = len(status.keys())

    for idx, user in enumerate(status.keys()):
        intervals = status[user]['intervals']

        for i in intervals:
            day1, hr1, min1 = i[0]
            day2, hr2, min2 = i[1]

            offset = idx / user_count
            left = (day1 + offset) / 7
            right = (day2 + offset + (1 / user_count)) / 7
            top = (1 + hr1 + min1 / 60) / 25
            bottom = (1 + hr2 + min2 / 60) / 25
            draw_rect((left, top, right, bottom), USER_COLORS[idx % 4])

        blackouts = status[user]['blackouts']
        for i in blackouts:
            day1, hr1, min1 = i[0]
            day2, hr2, min2 = i[1]

            offset = idx / user_count
            left = (day1 + offset) / 7
            right = (day2 + offset + (1 / user_count)) / 7
            top = (1 + hr1 + min1 / 60) / 25
            bottom = (1 + hr2 + min2 / 60) / 25
            draw_rect((left, top, right, bottom), BLACKOUT_COLOR)

    # grid lines
    ctx.strokeStyle = DIVIDER
    for i in range(1, 25):
        divider = (i / 25) * h
        ctx.beginPath()
        ctx.moveTo(0, divider)
        ctx.lineTo(w, divider)
        ctx.moveTo(w, divider + 1)
        ctx.lineTo(0, divider + 1)
        ctx.stroke()

    now = __new__(Date())
    hour = now.getHours() + (now.getMinutes() / 60)

    divider = ((hour + 1) / 25)

    draw_rect((0, divider - 0.005, 1, divider + 0.005), NOW_COLOR)
Beispiel #11
0
def getJsElementById(idstr: str):
    return document.getElementById(idstr)