CS 352: Computer Graphics: Input Interaction
CS 352: Computer Graphics: Input Interaction
Input
and
Interaction
Chapter 3 - 2
JS1k canvas examples: 1 2 3 4 5 Games, visual demosof what value? Is there a Christian perspective?
Chapter 3 - 3
Interaction
Much of the fun and utility of graphics is in interaction: accepting user input that affects the display Paint programs, modeling, games, word processors, spreadsheets, powerpoint
User initiates input events such as clicking on a menu or drawing a circle Computer response by changing graphical display
Chapter 3 - 4
Chapter 3 - 5
Projects 3
we'll learn 2-D graphics, interaction, event-loop programming, double-buffering simple animation, basic image processing Pen, line, and rectangle tools Color, pen size selectors Image processing (sharpen, blur, detect edges) Open, save images Toolbar and menu for controlling application
Features:
Chapter 3 - 6
to to to to
Chapter 3 - 7
How to paint?
Interactive prog:
Chapter 3 - 8
Input devices
Logical:
How to read?
Chapter 3 - 9
Input devices
Sample mode
Program reads the current state of input device (frequently) Each click or motion generates an event that goes on a queue Program can process events from queue
Event mode
HTML?
Chapter 3 - 10
Event-loop programming
Event loop:
Poll (or wait) for events Process events, update state Update display (ideally: wait for rest of frame time to elapse)
Chapter 3 - 11
State-driven
e.g. depressing the paintbrush tool releases other tools, puts me in "paint mode"
Good libraries will handle some of the bookkeeping for you You still typically have to handle some state
Chapter 3 - 12
State diagram
Chapter 3 - 13
HTML/JS provides event queue, support for many basic events (mousedown, mouseup
mouseover, mousemove, keypress, key release, value change, button click, etc.)
You are on your own for higher-level events, e.g. clicking on a toolbar tool It's also possible to set a function to run every 15 ms, sample input devices
Chapter 3 - 14
mousedown:
go into paint mode store mouse position draw initial dot exit paint mode if in paint mode
mouseup:
mousemove:
draw a line from old mouse position to current set old mouse position to current position
Chapter 3 - 15
Processing events
cpaint.drawStart = function(ev) { var x, y; x = ev.pageX - $(cpaint.canvas).offset().left; y = ev.pageY - $(cpaint.canvas).offset().top; ev.preventDefault(); cpaint.paintmode = true; cpaint.oldX = x; cpaint.oldY = y;
Chapter 3 - 16
Chapter 3 - 17
Chapter 3 - 18
Menus
<ul id="mainmenu"> <li>File <ul> <li id="menuNew">New</li> <li id="menuOpen">Open</li> <li id="menuSave">Save</li> ------------------------------------$('#menuNew').bind('click', cpaint.clear); $('#menuOpen').bind('click',cpaint.open); $('#menuSave').bind('click',cpaint.save);
Chapter 3 - 19
Toolbar
Chapter 3 - 20
Buttons
Widgets may have several states State should be evident in visual feedback E.g. how exactly does a button work? States?
Events: mousedown, mouseup, enter, exit Transitions: what happens in each state under each event?
Chapter 3 - 21
NH
Press Move
Chapter 3 - 22
Tooltips Whether button merely clicks and releases or can be selected Whether button can be unselected (e.g. B/I/U vs. Left, Center, Right)
Want consistent appearance, behavior over whole program or whole computer Really need a library implementation and a strict set of UI guidelines
Chapter 3 - 23
Chapter 3 - 24
$('.toolbarCell').each(function(index) { // unselect $(this).removeClass('selected'); // others }); var tool = '#' + cpaint.tool + 'Button'; // get ID $(tool).addClass('selected'); // select
Chapter 3 - 25
How?
Chapter 3 - 26
Color picker
Chapter 3 - 27
Rubber Banding
Chapter 3 - 28
Create your own off-screen canvas Copy it back on each mouse movement Events and actions?
Chapter 3 - 29
Mousedown
enter line mode remember mouse position as startx, starty save screen draw initial dot paste saved screen draw line from startx, starty to current mouse pos exit line mode
Mousemove
Mouseup
Chapter 3 - 30
Analysis
Drawbacks?
possible flickering
Chapter 3 - 31
Double buffering
Have two frame buffers, "front" and "back" Draw into back buffer At vertical retrace time, swap buffers
This is a fundamental graphics technique not built into canvas though, some drawing aggregation seems to happen automatically, behind the scenes; not usually necessary in canvas, but browser dependent
Chapter 3 - 32
Chapter 3 - 33
How to erase?
Chapter 3 - 34
Save, Load?
Save
Load
What are the security risks? Can only load files from the same server Can use a file chooser if it's a local app Security policies are not entirely in place
Chapter 3 - 35
Resizing
How would you allow the user to shrink, expand image? What would happen to image resolution?
Chapter 3 - 36
Replay commands to redraw scene Could store commands in a file (e.g. Mac PICT file) For some kinds of drawings, files are smaller and more accurate than bitmaps
Chapter 3 - 37
Other toolkits
HTML offers basic UI capabilities in a crossplatform context Writing your own UI extensions is tedious jQuery plugins extend capabilities Ideally, all UI elements ought to be built in In the real world, they come in platformspecific 'toolkits' or 'windowing libraries' E.g. MFC, QT, OpenLook, Cocoa
Chapter 3 - 38
Image processing
blur, sharpen detect edges enhance contrast noise reduction posterize fisheye redeye reduction find faces
Chapter 3 - 39
Chapter 3 - 40
Center on pixel of interest Multiply each value by color val underneath Add results Gaussian smoothing
Chapter 3 - 41
Gaussian smoothing
Chapter 3 - 42
Chapter 3 - 43
Chapter 3 - 44
Chapter 3 - 45
Sharpening
Subtract neighbors Like subtracting a blurred version of the image Unsharp Masking E.g. convolve with a kernel like one of these:
Chapter 3 - 46
Edge detection?
Wikipedia
Chapter 3 - 47
0
0 0
1
2 1
Could do this at different sales or resolutions Note: results maybe positive or negative numbers; must normalize
Chapter 3 - 48
Image Compression
What if you created say six different lowerresolution images and only stored the difference at each resolution?
Note: most of the data is in the highestfrequency component An early image compression technique
Chapter 3 - 49
(Wikipedia)
Chapter 3 - 50
DCT to convert to frequency domain Perceptual modeling Coefficient quantization Entropy coding
Chapter 3 - 51
Chapter 3 - 52
Summary
Event loop programming Interaction and event handling in HTML State diagrams Painting Rubber banding Double buffering Basic image processing and convolutions "Photoshop Nano"