示例#1
0
    def show(self):
        # If we've already shown this canvas elsewhere, don't create a new one,
        # just reuse it and scroll to the existing one.
        existing = self.get_element("")
        if existing is not None:
            self.draw_idle()
            existing.scrollIntoView()
            return

        # Disable the right-click context menu.
        # Doesn't work in all browsers.
        def ignore(event):
            event.preventDefault()
            return False

        window.addEventListener("contextmenu", ignore)

        # Create the main canvas and determine the physical to logical pixel
        # ratio
        canvas = document.createElement("canvas")
        context = canvas.getContext("2d")
        self._ratio = self.get_dpi_ratio(context)

        width, height = self.get_width_height()
        width *= self._ratio
        height *= self._ratio
        div = self.create_root_element()
        div.setAttribute(
            "style",
            "margin: 0 auto; text-align: center;" +
            "width: {}px".format(width / self._ratio),
        )
        div.id = self._id

        # The top bar
        top = document.createElement("div")
        top.id = self._id + "top"
        top.setAttribute("style", "font-weight: bold; text-align: center")
        top.textContent = self._title
        div.appendChild(top)

        # A div containing two canvases stacked on top of one another:
        #   - The bottom for rendering matplotlib content
        #   - The top for rendering interactive elements, such as the zoom
        #     rubberband
        canvas_div = document.createElement("div")
        canvas_div.setAttribute("style", "position: relative")

        canvas.id = self._id + "canvas"
        canvas.setAttribute("width", width)
        canvas.setAttribute("height", height)
        canvas.setAttribute(
            "style",
            "left: 0; top: 0; z-index: 0; outline: 0;" +
            "width: {}px; height: {}px".format(width / self._ratio,
                                               height / self._ratio),
        )
        canvas_div.appendChild(canvas)

        rubberband = document.createElement("canvas")
        rubberband.id = self._id + "rubberband"
        rubberband.setAttribute("width", width)
        rubberband.setAttribute("height", height)
        rubberband.setAttribute(
            "style",
            "position: absolute; left: 0; top: 0; z-index: 0; " +
            "outline: 0; width: {}px; height: {}px".format(
                width / self._ratio, height / self._ratio),
        )
        # Canvas must have a "tabindex" attr in order to receive keyboard
        # events
        rubberband.setAttribute("tabindex", "0")
        # Event handlers are added to the canvas "on top", even though most of
        # the activity happens in the canvas below.
        rubberband.addEventListener("mousemove", self.onmousemove)
        rubberband.addEventListener("mouseup", self.onmouseup)
        rubberband.addEventListener("mousedown", self.onmousedown)
        rubberband.addEventListener("mouseenter", self.onmouseenter)
        rubberband.addEventListener("mouseleave", self.onmouseleave)
        rubberband.addEventListener("keyup", self.onkeyup)
        rubberband.addEventListener("keydown", self.onkeydown)
        context = rubberband.getContext("2d")
        context.strokeStyle = "#000000"
        context.setLineDash([2, 2])
        canvas_div.appendChild(rubberband)

        div.appendChild(canvas_div)

        # The bottom bar, with toolbar and message display
        bottom = document.createElement("div")
        toolbar = self.toolbar.get_element()
        bottom.appendChild(toolbar)
        message = document.createElement("div")
        message.id = self._id + "message"
        message.setAttribute("style", "min-height: 1.5em")
        bottom.appendChild(message)
        div.appendChild(bottom)

        self.draw()
示例#2
0
    def show(self):
        # If we've already shown this canvas elsewhere, don't create a new one,
        # just reuse it and scroll to the existing one.
        existing = self.get_element('')
        if existing is not None:
            self.draw_idle()
            existing.scrollIntoView()
            return

        # Disable the right-click context menu.
        # Doesn't work in all browsers.
        def ignore(event):
            event.preventDefault()
            return False
        window.addEventListener('contextmenu', ignore)

        # Create the main canvas and determine the physical to logical pixel
        # ratio
        canvas = document.createElement('canvas')
        context = canvas.getContext('2d')
        self._ratio = self.get_dpi_ratio(context)

        width, height = self.get_width_height()
        width *= self._ratio
        height *= self._ratio
        div = self.create_root_element()
        div.setAttribute('style', 'width: {}px'.format(width / self._ratio))
        div.id = self._id

        # The top bar
        top = document.createElement('div')
        top.id = self._id + 'top'
        top.setAttribute('style', 'font-weight: bold; text-align: center')
        top.textContent = self._title
        div.appendChild(top)

        # A div containing two canvases stacked on top of one another:
        #   - The bottom for rendering matplotlib content
        #   - The top for rendering interactive elements, such as the zoom
        #     rubberband
        canvas_div = document.createElement('div')
        canvas_div.setAttribute('style', 'position: relative')

        canvas.id = self._id + 'canvas'
        canvas.setAttribute('width', width)
        canvas.setAttribute('height', height)
        canvas.setAttribute(
            'style', 'left: 0; top: 0; z-index: 0; outline: 0;' +
            'width: {}px; height: {}px'.format(
                width / self._ratio, height / self._ratio)
        )
        canvas_div.appendChild(canvas)

        rubberband = document.createElement('canvas')
        rubberband.id = self._id + 'rubberband'
        rubberband.setAttribute('width', width)
        rubberband.setAttribute('height', height)
        rubberband.setAttribute(
            'style', 'position: absolute; left: 0; top: 0; z-index: 0; outline: 0;' +
            'width: {}px; height: {}px'.format(
                width / self._ratio, height / self._ratio)
        )
        # Canvas must have a "tabindex" attr in order to receive keyboard events
        rubberband.setAttribute('tabindex', '0')
        # Event handlers are added to the canvas "on top", even though most of the
        # activity happens in the canvas below.
        rubberband.addEventListener('mousemove', self.onmousemove)
        rubberband.addEventListener('mouseup', self.onmouseup)
        rubberband.addEventListener('mousedown', self.onmousedown)
        rubberband.addEventListener('mouseenter', self.onmouseenter)
        rubberband.addEventListener('mouseleave', self.onmouseleave)
        rubberband.addEventListener('keyup', self.onkeyup)
        rubberband.addEventListener('keydown', self.onkeydown)
        context = rubberband.getContext('2d')
        context.strokeStyle = '#000000';
        context.setLineDash([2, 2])
        canvas_div.appendChild(rubberband)

        div.appendChild(canvas_div)

        # The bottom bar, with toolbar and message display
        bottom = document.createElement('div')
        toolbar = self.toolbar.get_element()
        bottom.appendChild(toolbar)
        message = document.createElement('div')
        message.id = self._id + 'message'
        message.setAttribute('style', 'min-height: 1.5em')
        bottom.appendChild(message)
        div.appendChild(bottom)

        self.draw()
示例#3
0
            L.input('#toggle-all.toggle-all', type="checkbox", onChange=toggle_all,
                checked=remaining == 0 and total > 0),
            L.label(htmlFor="toggle-all") / 'Mark all as complete',
        ),
        L.ul('.todo-list') / todos,
        L.footer('.footer') / (
            L.span('.todo-count') / (
                L.strong / f"{remaining} ",
                f"{'item' if remaining == 1 else 'items'} left",
            ),
            L.ul('.filters') / (
                L.li / L.a('.selected' if filter == 'all' else '', href="#") / 'All',
                L.li / L.a('.selected' if filter == 'active' else '', href="#active") / 'Active',
                L.li / L.a('.selected' if filter == 'completed' else '', href="#completed") / 'Completed',
            ),
            (L.button('.clear-completed', onClick=clear_completed) / 'Clear completed') if completed else None,
        ),
    ))

    ReactDOM.render(
      rendered,
      document.getElementById('app')
    );


if localStorage.getItem('app'):
    STATE = json.loads(localStorage.getItem('app'))

render()
window.addEventListener('hashchange', lambda *args: render())