{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import Image\n", "\n", "from ipywidgets import ColorPicker, IntSlider, link, AppLayout, HBox\n", "\n", "from ipycanvas import RoughCanvas, hold_canvas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "width = 1000\n", "height = 1000" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = RoughCanvas(width=width, height=height)\n", "\n", "drawing = False\n", "position = None\n", "shape = []\n", "\n", "\n", "def on_mouse_down(x, y):\n", " global drawing\n", " global position\n", " global shape\n", "\n", " drawing = True\n", " position = (x, y)\n", " shape = [position]\n", "\n", "\n", "def on_mouse_move(x, y):\n", " global drawing\n", " global position\n", " global shape\n", "\n", " if not drawing:\n", " return\n", "\n", " with hold_canvas():\n", " canvas.stroke_line(position[0], position[1], x, y)\n", "\n", " position = (x, y)\n", "\n", " shape.append(position)\n", "\n", "\n", "def on_mouse_up(x, y):\n", " global drawing\n", " global position\n", " global shape\n", "\n", " drawing = False\n", "\n", " with hold_canvas():\n", " canvas.stroke_line(position[0], position[1], x, y)\n", " canvas.fill_polygon(shape)\n", "\n", " shape = []\n", "\n", "\n", "canvas.on_mouse_down(on_mouse_down)\n", "canvas.on_mouse_move(on_mouse_move)\n", "canvas.on_mouse_up(on_mouse_up)\n", "\n", "canvas.stroke_style = \"#749cb8\"\n", "\n", "picker = ColorPicker(description=\"Color:\", value=\"#749cb8\")\n", "\n", "link((picker, \"value\"), (canvas, \"stroke_style\"))\n", "link((picker, \"value\"), (canvas, \"fill_style\"))\n", "\n", "HBox((canvas, picker))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }