Results 1 to 8 of 8

Thread: [RESOLVED] Can't get canvas.setTransform to work :(

  1. #1
    Join Date
    Jul 2010

    resolved [RESOLVED] Can't get canvas.setTransform to work :(

    Hello all. I'm still playing around with HTML5 + JS, and while i have made a certain progress, i still don't get a lot about it.

    I resorted to ripping off other people's work, and found this awesome canvas example: http://www.html5.jp/blog/contents/HT...rm-jquery.html.
    It works great if i leave it at this:
    ctx.clearRect(-w*2, -h*2, w*4, h*4);
    ctx.setTransform(p.m11, p.m12, p.m21, p.m22, p.dx, p.dy);
    ctx.drawImage(img, 0, 0);
    But as soon as i leave out the drawImage part and substitute it with putImageData i previously created (remember, i have a dynamically generated thing, it's only on canvas, not in an Image object that i could use for drawImage), it stops scaling! I'm back to the big, 100% scaled image.

    PLEASE help me out with this, these canvas and their logic are driving me nuts!!!!

    P.S. Also, why do we save the state before making any changes, then restore the state after the changes, and somehow still see the changes made??? Shouldn't the restore() method revert to the state before any changes were made??

    I'm eagerly awaiting for anyone to shed some light on this

  2. #2
    Join Date
    Jan 2003

    I don't claim to be an expert on Canvas, but I think we can figure it out. ;-)

    putImageData, it would be easier for me to understand what the problem is if you had a link so I could see how you're using it. It's a little hard to answer that otherwise.

    Your question about save state / restore state has to do with the behavior of Canvas. When you transform the canvas without clearing it and redrawing it, the canvas remains the same and new changes are placed on top of it. In other words, if you took out the save/clear/restore lines, and made the code look like the following, you would see duplicate copies of the image on top of one another:

    	//ctx.clearRect(-w*2, -h*2, w*4, h*4);
    	ctx.setTransform(p.m11, p.m12, p.m21, p.m22, p.dx, p.dy);
    	ctx.drawImage(img, 0, 0);
    In that case, changing the parameters of the setTransform method would duplicate the canvas view in addition to modifying it.

    The way to work around this is, of course, to save the canvas context in its current state (which is why we start with the
    save() method), clear what is currently visible on the canvas (clearRect()), perform our transformation operation, and then when we're done, restore the canvas state (restore()). The save/restore methods don't actually clear/restore the output of the canvas itself (that's what drawImage does); however, they do provide state-change information (the "context" as it were), which is important for a program that will be interacting with the canvas (particularly for optimizing redraws and the creation of new paths after they have been filled or stroked). Essentially, the transformations performed on the canvas are cumulative, and therefore each call saves and restores the canvas state. Does that help any?
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    Jul 2010

    thank you so much for explaining canvas to me! my biggest mistake was that i thought that the save() and restore() methods saved/restored the pixel data of canvas. now i understand that, apparently, they save/restore the, er, attributes of canvas, such as the transform arguments.

    however, now it seems clearer to me that the resize trick may be impossible to implement at all.
    i have put together a small example: http://gda.0fees.net/tests/test01.php
    function resize1() works just great, but it redraws the image from a static source. this is not what i need
    function resize2() calls putImageData(), and apparently this method disregards the transformed state of canvas...

    P. S. sorry i'm so slow. i received the notification about your answer immediately, but things have been keeping me really busy lately...

  4. #4
    Join Date
    Jul 2010
    i have found a workaround that uses the drawImage function. it's kinda dodgy though.
    var tempImage = new Image;
    /* draw something on the canvas here */
    tempImage.src = $("tc").toDataURL("image/png");
    $("tc").getContext("2d").clearRect(0, 0, 604, 339);
    $("tc").getContext("2d").drawImage(tempImage, 0, 0, 302, 170)
    This does scale the image inside the canvas to 50%, but it's not exactly lightning fast. in fact, it appears to be several times slower than using putImageData() (which doesn't work, however).
    I only wonder whether i will have any speed boost if i say screw it and implement bicubic interpolation of the imageData array

  5. #5
    Join Date
    Jan 2003

    Why not simply draw the image to the canvas at 50% of the canvas dimensions? No need for a transform operation at all, and probably faster, too.

    var ctx = $('tc').getContext('2d');
     ctx.clearRect(0, 0, 604, 339);
     ctx.drawImage($('ti'), 0, 0, 604 / 2, 339 / 2);
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  6. #6
    Join Date
    Jul 2010
    Then it will always draw the original image, as contained in the file.

    Sorry that my example page doesn't reflect it, but instead of drawing a PNG file there actually should be drawing some graphs and stuff rendered in real time. I can't fetch it from a file, it's content created on the fly.
    And also i apologize that i can't post what exactly i'm working with and need to show this by way of example pages - my work is slightly confidential as of now

  7. #7
    Join Date
    Jan 2003
    The image being drawn to the canvas is pixel data, so you're going to want to operate on that before drawing it to the canvas, rather than drawing it to the canvas and then performing your operations.

    In the following example code, you can reference
    imgD.data to modify the pixel data directly.

    var ctx = $('tc').getContext('2d');
     ctx.clearRect(0, 0, 604, 339);
     ctx.drawImage($('ti'), 0, 0, 604, 339);
     var imgD = ctx.getImageData(0, 0, 604, 339);
     ctx.putImageData(imgD, 0, 0);
    Here's a very simplified example that increases the exposure (contrast) of the image.

    var ctx = $('tc').getContext('2d');
     ctx.clearRect(0, 0, 604, 339);
     ctx.drawImage($('ti'), 0, 0, 604, 339);
     var imgD = ctx.getImageData(0, 0, 604, 339);
     // this loop should be extracted to a separate function
     for (var x = 0; x < imgD.data.length; x++){
      imgD.data[x] = imgD.data[x] << 2;
     ctx.putImageData(imgD, 0, 0);
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  8. #8
    Join Date
    Jul 2010
    Yeah, i guess i will work in this direction, even though it will reduce the flexibility of my script.

    And Jona, big thanks for helping me out! - before you explained things to me, i hadn't understood canvas at all, and now i do!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.21504 seconds
  • Memory Usage 2,920KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates