`
sillycat
  • 浏览: 2492559 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

HTML5(五) Picture and Text

    博客分类:
  • UI
阅读更多
HTML5(五) Picture and Text

1. image object
Image is for a picture, we have many ways to get a image object.
a. document.images array
b. document.getElementsByTagName or document.getElementById 
c. create a new Image object, and set a picture URL
   var img = new Image();
   img.src = 'my_image.png';
d. data:url
encode all the picture data via BASE64 to /9j/4QDfRXhpZgAASUkqAAgAAAAFABIBAwABAAAAAQAAADEBAgAVAAAASgAAADIBAgAUAAAAXw
and put them all in the html

2. drawing object
drawImage(image,x,y)
(x,y) is the left top point of the picture, it decides the postion

my example of the small picture, test5.html:
<canvas id="canvas1" width="250" height="300" style="background-color:black">
    you are out!
</canvas><br/>
<input type="button" value="show the girl" onclick="Show();" />
<input type="button" value="hide" onclick="Clear();" />

<script type="text/javascript">
    //picture data Base64 encode
    IMG_SRC='data:image/gif;base64,/9j/4QDfRXhpZgAASUkqAAgAAAA......'//省略40字节
    //small
    IMG_SRC_SMALL='data:image/gif;base64,/9j/4QDfRXhpZgAASUkqAAgAAAAFABIBAwABAAAAAQAAADEBAgAVAAAASgAAADIBAgAUAAAAXwAAABMCAwABAAAAAQAAAGmHBAABAAAAcwAAAAAAAABBQ0QgU3lzdGVtcyDK/cLrs8nP8QAyMDEwOjEwOjA0IDE1OjM5OjU3AAUAAJAHAAQAAAAwMjIwkJICAAQAAAAyODEAAqAEAAEAAAAXAAAAA6AEAAEAAAAeAAAABaAEAAEAAAC1AAAAAAAAAAIAAQACAAQAAABSOTgAAgAHAAQAAAAwMTAwAAAAAAAAAAD//gAqSW50ZWwoUikgSlBFRyBMaWJyYXJ5LCB2ZXJzaW9uIDEsNSw0LDM2AP/AABEIAB4AFwMBIQACEQEDEQH/2wCEAAMCAgICAQMCAgIDAwMDBAcEBAQEBAkGBgUHCgkLCwoJCgoMDREODAwQDAoKDxQPEBESExMTCw4VFhUSFhESExIBBAUFBgUGDQcHDRsSDxIbG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document.getElementById("canvas1").getContext("2d");
        img=new Image();
        img.onload=function(){
            ctx.drawImage(img,0,0);
        }
        img.src=IMG_SRC_SMALL;  
    }

    function Clear(){
        ctx = document.getElementById("canvas1").getContext("2d");
        ctx.clearRect(0,0,250,300);
    } 
</script>

3. enlarge and make it small
drawImage(image,x,y,width,height)

my test page is test5-1.html:
<canvas id="canvas2" width="250" height="300" style="background-color:black">
    you are out
</canvas><br/>
horizontal<input type="range" min="1" max="200" onchange="Scale1(event)"/><br/>
vertical<input type="range" min="1" max="200" onchange="Scale2(event)"/><br/>
percent<input type="range" min="1" max="200" onchange="Scale3(event)"/><br/>
tile<input type="range" min="1" max="100" value="1" onchange="Scale4(event)"/><br/>

<script type="text/javascript">   

    function Scale1(){
        //caculate
        var scale=event.target.value/10
        ctx = document.getElementById("canvas2").getContext("2d");
        ctx.clearRect(0,0,250,300);
        img=new Image();
        img.onload=function(){
            ctx.drawImage(img,0,0,img.width*scale,img.height);
        }
        img.src=IMG_SRC_SMALL;
    }
       
    function Scale2(){
        var scale=event.target.value/10
        ctx = document.getElementById("canvas2").getContext("2d");
        ctx.clearRect(0,0,250,300);
        img=new Image();
        img.onload=function(){
            ctx.drawImage(img,0,0,img.width,img.height*scale);
        }
        img.src=IMG_SRC_SMALL;
    }
   
    function Scale3(){
        var scale=event.target.value/10
        ctx = document.getElementById("canvas2").getContext("2d");
        ctx.clearRect(0,0,250,300);
        img=new Image();
        img.onload=function(){
            ctx.drawImage(img,0,0,img.width*scale,img.height*scale);
        }
        img.src=IMG_SRC_SMALL;
    }
   
    function Scale4(){
        var scale=event.target.value;
        ctx = document.getElementById("canvas2").getContext("2d");
        ctx.clearRect(0,0,250,300);
        img=new Image();
        img.onload=function(){
            var n1=img.width/scale;
            var n2=img.height/scale;
           
            for(var i=0;i<n1;i++)
                for(var j=0;j<n2;j++)
                    ctx.drawImage(img,i*img.width/scale,j*img.height/scale,img.width/scale,img.height/scale);
        }
        img.src=IMG_SRC_SMALL;
    }
</script>

4. cut the image
drawImage(image,sx,sy,sWidth,sHeight,dx,dy,dWidth,dHeight)
(sx,sy) the start point of the cut picture
sWidth and sHeight are the width and height of the cut picture.

(dx,dy) the start point of the output picture
dWidth and dHeight are the width and height of the output picture.

5. Image
createPattern(image,type)
type: repeat, repeat-x, repeat-y, no-repeat
my example file is test5-2.html:
<canvas id="canvas3" width="250" height="300" style="background-color:black">
    you are out!
</canvas><br/>
<input type="button" value="Go" onclick="Patterns();" />
<input type="button" value="Clear" onclick="ClearPatterns();" />

<script type="text/javascript">
        function Patterns(){
        ctx = document.getElementById("canvas3").getContext("2d");
        img=new Image();
        img.src=IMG_SRC_SMALL;
        img.onload=function(){
            var ptrn = ctx.createPattern(img,'repeat');
            ctx.fillStyle = ptrn; 
            ctx.fillRect(0,0,250,300); 
        }
    }
   
    function ClearPatterns(){
        ctx = document.getElementById("canvas3").getContext("2d");
        ctx.clearRect(0,0,250,300);
    }
</script>

6.Text
font: text style, it is like font-family in CSS
textAlign:   start, end, left, right, center, default value is start.
textBaseline:  top, hanging, middle, alphabetic, ideographic, bottom. default value is alphabetic.

fillText
strokeText

my example file is test5-3.html:
<canvas id="canvas3" width="250" height="300" style="background-color:black">
    you are out!
</canvas><br/>
<input type="button" value="Show" onclick="Show();" />
<input type="button" value="Clear" onclick="ClearPatterns();" />

<script type="text/javascript">
   
    function Show(){
        ctx = document.getElementById("canvas3").getContext("2d");
        ctx.fillStyle    = 'white';
ctx.font         = 'italic 30px sans-serif';
ctx.textBaseline = 'top';
ctx.fillText('Hello world1!', 0, 0);
ctx.font         = 'italic 30px sans-serif';
ctx.fillText('Hello world2!', 0, 50);
    }
   
    function ClearPatterns(){
        ctx = document.getElementById("canvas3").getContext("2d");
        ctx.clearRect(0,0,250,300);
    }
</script>

references:
http://www.blogjava.net/myqiao/category/46360.html
分享到:
评论

相关推荐

    NET WYSIWYG HTML Editor(htlm可视化编辑器)汉化版

    text background color, numeric items, symbol items, decrease indent, increase indent, align left, align center, align right, add hyperlink, remove hyperlink, add image, insert horizontal line, set and...

    AdRevenue.v2.0

    With adRevenue, you or your advertisers can start serving and managing Text Ads, Banners, Picture+Text Ads and Freeform HTML in multiple zones on your website in no time at all. Profit from the hard ...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持Delphi 4-XE5 and C++Builder 6-XE5. D2010以上版本(D14_D19)安装必读 delphi2010以上版本(D14_D19)使用者安装时,请将res\frccD14_...

    Web Development with JavaServer Pages

    A larger example, a Web database of frequently asked questions (FAQs), demonstrates the big picture with JSPs and beans. Final chapters turn toward a useful aspect of JSP, custom tags, which allow ...

    VB编程资源大全(英文控件)

    that scrolls text&lt;END&gt;&lt;br&gt;81,mtymse.zip This is a control (VB5 with source) that controls every aspect of the mouse&lt;END&gt;&lt;br&gt;82,progbar.zip Uses a picture box to emulate a progress bar&lt;END&gt;&lt;br&gt;83...

    exercise1 答案

    When you find a web site that you would like to use, copy and paste its URL into a text file (You can use Notepad as your text editor) instead of retyping the URL. This approach saves time and reduces...

    VB编程资源大全(英文源码 网络)

    So 下载 the code and get involved with the News Group, help us to help you.&lt;END&gt;&lt;br&gt;4 , urllink.zip User control to launch web browser and jump to URL.&lt;END&gt;&lt;br&gt;5 , vbftp.zip Sample application ...

    VB编程资源大全(英文源码 控制)

    start your Visual Basic program with your project code showing, right click and you should see "Rem Builder".&lt;END&gt;&lt;br&gt;3 , syntax.zip This is an excellent example of how to highlight HTML code in a...

    WordPress 2.8 Theme Design.pdf

    Optimizing for text and mobile browsers 140 The new mobile Safari browser 141 Summary 141 Chapter 5: Putting Your Theme into Action 143 A picture's worth 143 Theme packaging basics 145 ...

    VB编程资源大全(英文源码 其它)

    It allows you to drag and drop text, html & rtf files onto the executable and it will create the files in the correct format.&lt;END&gt;&lt;br&gt;41,randomchars.zip This set of routines and sample project ...

    FastReport.v4.9.81 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版

    Wide range of objects that can be used in a report: text, picture, lines, shapes, charts, barcodes, cross-table, ole object, richtext, checkbox, gradient. screenshot Visual report designer supports ...

    ASCII Art Generator 3.2.4.4 by TeamND

    ASCII Art Generator is an amazing graphics art to text art solution, which allows you to convert digital pictures into full color text-based images easily and quickly. You can input any message you’d...

    EdrawSoft Edraw Max 7.2.0.2467 流程图作图工具

    • Recolor picture, Light and Contrast, transparent PNG support. • Opened the Shape Sheet for senior users to create more complicated shapes. • More 2000 clip arts. • Improved the Insert Hyperlink ...

    chorify:物联网黑客马拉松项目

    Users can take a picture of cleaned up bed and get rewarded3. Users can create their own profiles and view points earned by their peers.4. Users can either send email or text reiminders to do the ...

    b2evo-captcha-1.3.1

    echo '$imgLoc.'" alt="This is a captcha-picture. It is used to prevent mass-access by robots." title=""&gt; '."\n"; echo '$_SERVER['PHP_SELF'].'" method="POST"&gt;'."\n"; echo '$imgLoc.'"&gt;'."\n"; ...

    ICS delphixe10源码版

    picture postcard showing the area you live in and some beautiful stamps for my kids who are stamp collectors. Do not use an envelop, I collect USED postcards sent to me. Write on the postcard that it ...

    word自动转化为chm

    You may choose to open the Word document manual and save it as “Web File” as the picture showing below. Please note: Do not save it as “Single File Web Page” or “Web Page, Filtered”. Then you ...

    WordPress 2.7 Cookbook.pdf

    Step 1: PHP and HTML 244 Step 2: The CSS 245 Step 3: Optional JavaScript 246 How it works 247 There's more... 247 Creating a horizontal drop-down menu 247 Adding a breadcrumb to your theme 250 ...

    编译好的x265,带y4m文件

    -h/--help Show this help text and exit -V/--version Show version info and exit Output Options: -o/--output &lt;filename&gt; Bitstream output file name --log-level &lt;string&gt; Logging level: none error ...

    js使用小技巧

    &lt;input type=text name=text1 value="123" onfocus="this.select()"&gt; ENTER键可以让光标移到下一个输入框 (event.keyCode==13)event.keyCode=9"&gt; 文本框的默认值 &lt;input type=text value="123" onfocus="alert...

Global site tag (gtag.js) - Google Analytics