{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Rotating Cube\n",
    "\n",
    "This is a notebook version of the \"examples/basics/visuals/cube.py\" example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import vispy\n",
    "vispy.use(\"jupyter_rfb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "from vispy import app, gloo\n",
    "from vispy.visuals import CubeVisual, transforms\n",
    "\n",
    "\n",
    "class Canvas(app.Canvas):\n",
    "    def __init__(self):\n",
    "        app.Canvas.__init__(self, 'Cube', keys='interactive',\n",
    "                            size=(400, 400))\n",
    "\n",
    "        self.cube = CubeVisual((1.0, 0.5, 0.25), color='red', edge_color=\"k\")\n",
    "        self.theta = 0\n",
    "        self.phi = 0\n",
    "\n",
    "        # Create a TransformSystem that will tell the visual how to draw\n",
    "        self.cube_transform = transforms.MatrixTransform()\n",
    "        self.cube.transform = self.cube_transform\n",
    "\n",
    "        self.timer = app.Timer('auto', connect=self.on_timer, start=True)\n",
    "\n",
    "    def on_resize(self, event):\n",
    "        # Set canvas viewport and reconfigure visual transforms to match.\n",
    "        vp = (0, 0, self.physical_size[0], self.physical_size[1])\n",
    "        self.context.set_viewport(*vp)\n",
    "        self.cube.transforms.configure(canvas=self, viewport=vp)\n",
    "\n",
    "    def on_draw(self, event):\n",
    "        gloo.set_viewport(0, 0, *self.physical_size)\n",
    "        gloo.clear('white', depth=True)\n",
    "\n",
    "        self.cube.draw()\n",
    "\n",
    "    def on_timer(self, event):\n",
    "        self.theta += .5\n",
    "        self.phi += .5\n",
    "        self.cube_transform.reset()\n",
    "        self.cube_transform.rotate(self.theta, (0, 0, 1))\n",
    "        self.cube_transform.rotate(self.phi, (0, 1, 0))\n",
    "        self.cube_transform.scale((100, 100, 0.001))\n",
    "        self.cube_transform.translate((200, 200))\n",
    "        self.update()\n",
    "\n",
    "\n",
    "win = Canvas()\n",
    "win.show()\n",
    "win"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above implementation uses a timer to continuously update the coordinates of the cube so it appears to rotate. This will go until the timer is stopped which you can do by running:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "win.timer.stop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can restart the rotation by calling the `.start()` method on the timer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "win.timer.start()"
   ]
  },
  {
   "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.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
