{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# RoughCanvas\n", "\n", "The RoughCanvas give a nice hand-drawn style to your canvas!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipycanvas import RoughCanvas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Draw shapes\n", "\n", "The rough canvas gives a nice hand-drawn style to your canvas, **note** that it only works with direct draw calls, it does not work with paths created with `begin_path`, those paths will be drawned normally." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "canvas = RoughCanvas()\n", "\n", "canvas.stroke_rect(100, 100, 100, 100)\n", "canvas.fill_rect(50, 50, 100, 100)\n", "\n", "canvas.stroke_circle(300, 300, 100)\n", "canvas.fill_circle(350, 350, 100)\n", "\n", "canvas.stroke_line(200, 200, 300, 300)\n", "\n", "canvas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fill styles\n", "\n", "With the RoughCanvas, not only you can modify the fill color but you can also modify the fill styling, using one of the following values: 'hachure' (default), 'solid', 'zigzag', 'cross-hatch', 'dots', 'sunburst', 'dashed' or 'zigzag-line'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = RoughCanvas(width=850, height=100)\n", "\n", "canvas.fill_style = \"blue\"\n", "canvas.line_width = 2.0\n", "\n", "rough_fill_style_values = [\n", " \"hachure\",\n", " \"solid\",\n", " \"zigzag\",\n", " \"cross-hatch\",\n", " \"dots\",\n", " \"sunburst\",\n", " \"dashed\",\n", " \"zigzag-line\",\n", "]\n", "\n", "for i in range(len(rough_fill_style_values)):\n", " canvas.rough_fill_style = rough_fill_style_values[i]\n", "\n", " canvas.fill_rect(10 + i * 100, 10, 90, 80)\n", "\n", "\n", "canvas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stroke styles\n", "\n", "You can still change the stroke width using the `line_width` attribute, just like with the normal Canvas class" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = RoughCanvas(width=600, height=300)\n", "\n", "canvas.line_width = 3.0\n", "canvas.stroke_rect(100, 100, 100, 100)\n", "\n", "canvas.line_width = 10.0\n", "canvas.stroke_rect(300, 100, 100, 100)\n", "\n", "canvas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sketch options\n", "\n", "There are some options you can play with to change the style of your drawing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Roughness: value indicating how rough the drawing is" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = RoughCanvas(width=850, height=100)\n", "\n", "canvas.fill_style = \"green\"\n", "\n", "for i in range(8):\n", " canvas.roughness = i\n", "\n", " canvas.fill_rect(10 + i * 100, 10, 90, 80)\n", "\n", "\n", "canvas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Bowing: value indicating how curvy the lines are when drawing a sketch" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas = RoughCanvas(width=850, height=150)\n", "\n", "canvas.fill_style = \"green\"\n", "\n", "for i in range(8):\n", " canvas.bowing = i * 3.0\n", "\n", " canvas.stroke_rect(20 + i * 100, 20, 90, 110)\n", "\n", "\n", "canvas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make a rough Pie chart" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import pi\n", "\n", "c = RoughCanvas(width=600, height=600)\n", "\n", "c.fill_style = \"green\"\n", "c.fill_arc(300, 300, 200, 0, 0.9 * pi)\n", "\n", "c.fill_style = \"red\"\n", "c.fill_arc(300, 300, 200, 0.9 * pi, pi + 0.2 * pi)\n", "\n", "c.fill_style = \"blue\"\n", "c.fill_arc(300, 300, 200, pi + 0.2 * pi, 2 * pi)\n", "\n", "c.stroke_style = \"black\"\n", "c.stroke_arc(300, 300, 200, 0, 2 * pi)\n", "\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Draw thousands of shapes at once" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_particles = 3_000\n", "\n", "import numpy as np\n", "\n", "x = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)\n", "y = np.array(np.random.rayleigh(250, n_particles), dtype=np.int32)\n", "size = np.random.randint(4, 8, n_particles)\n", "\n", "canvas = RoughCanvas(width=800, height=500)\n", "\n", "canvas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas.fill_rects(x, y, size)" ] } ], "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.9.0" } }, "nbformat": 4, "nbformat_minor": 4 }