Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adding basic raster drawing
  • Loading branch information
int-0 committed Jan 1, 2018
commit 8c10149ab22d22d69393d230a2a07ab8588220ed
51 changes: 51 additions & 0 deletions examples/canvas_raster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
"""
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import os
import remi.gui as gui
from remi.game import Color, Rect
from remi.game.canvas import Canvas
from remi.game.raster import load_image, draw
from remi import start, App


class MyApp(App):
canvas = None
def __init__(self, *args):
super(MyApp, self).__init__(*args)

def main(self, name='world'):
#margin 0px auto allows to center the app to the screen
container = gui.Widget(width=600, height=600)
self.canvas = Canvas(self, resolution=(600, 400), margin='0px auto')
button = gui.Button('Go!')
button.set_on_click_listener(self.draw)
container.append(self.canvas)
container.append(button)
# returning the root widget
return container

def draw(self, widget):
image = load_image('example.png')
draw(image, self.canvas, position=(10, 10))

if __name__ == "__main__":
print 'Starting with pid: %s' % os.getpid()
# starts the webserver
# optional parameters
# start(MyApp,address='127.0.0.1', port=8081, multiple_instance=False,enable_file_cache=True, update_interval=0.1, start_browser=True)
start(MyApp, debug=True)
1 change: 0 additions & 1 deletion remi/game/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ def id(self):
return self.attributes['id']

def draw(self, js_code):
print js_code
self._app.execute_javascript(js_code)
34 changes: 34 additions & 0 deletions remi/game/raster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

from PIL import Image


class RasterImage(object):
data = ''
width = 0
height = 0


def load_image(image_file):
image = Image.open(image_file)
data = image.tobytes()
data = [ord(b) for b in data]
result = RasterImage()
result.width = image.width
result.height = image.height
result.data = repr(data)
return result


def draw(image, canvas, position):
canvas.draw('''var canvas = document.getElementById('%s');
var ctx = canvas.getContext('2d');
var image = ctx.createImageData(%s, %s);
image.data.set(new Uint8ClampedArray(%s));
ctx.putImageData(image, %s, %s);''' % (
canvas.id,
image.width, image.height, image.data,
position[0], position[1]
))