doushan6161 2014-10-27 20:50
浏览 27

高级自定义字段,设置html5画布文本

If anyone would have any references or links to point me in the direction I'd greatly appreciate it. I know I can set canvas text with "context.fillText ("Hello World");" when just doing straight forward canvas.

My question is how would I go about using Advanced Custom Fields (acf) field to as my text variable so the client can set/change the text on their own.

So if I had:

<?php the_field('canvas_text'); ?>

How would I go about setting this as the 'fillText'?

Could I use somehow use the ACF field as a variable that I then use in the canvas js?

  • 写回答

1条回答 默认 最新

  • douyimin1083 2014-11-07 11:11
    关注

    Use wp_localize_script to pass a variable from php to javascript in WordPress, like this:

    wp_localize_script( 'canvas_js', 'canvas_js_localization', array(
        'canvas_text' => get_field( 'canvas_text' )
    ) );
    

    and then use it in your js like this:

    context.fillText (canvas_js_localization.canvas_text)
    

    Of course you have to edit the handle and name to fit your scenario.

    评论

报告相同问题?