HTML5 Canvas ( 图片绘制 转化为base64 )

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>canvas</title>
    <script type="text/javascript" src="../js/jQuery.js"></script>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
            outline: none;
            border: none;
        }
        #canvas{
            margin: auto auto;
            width: 7rem;
            margin: .25rem 0 0 1.5rem;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="1000" height="600"></canvas>
    <img id="img" src="" />
</body>
</html>
<script type="text/javascript">
    /**
     * rem 布局初始化
     */
    $(‘html‘).css(‘font-size‘, $(window).width()/10);
    /**
     * 获取 canvas 画布
     * 获取 canvas 绘图上下文环境
     */
    var canvas = $(‘#canvas‘)[0];
    var cxt = canvas.getContext(‘2d‘);

    /**
     * 图片处理函数 drawImage, 图片转化为 base64 格式 toDataURL
     * drawImage(图形对象, sx, sy, sw, sh, dx, dy, dw, dh)
     * s: 原图形
     * d: 新图形
     * x: 图形的起点横坐标
     * y: 图形的起点纵坐标
     * w: 图形的宽度
     * h: 图形的高度
     * toDataURL("图片格式, 默认为 image/png")
     */
    var img = new Image();
    img.src = "../img/background_2.jpg";
    img.onload = function(){
        cxt.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
        var src = canvas.toDataURL("image/jpeg");
        $(‘#img‘).attr(‘src‘, src);
    }
</script>
时间: 2024-12-22 01:32:13

HTML5 Canvas ( 图片绘制 转化为base64 )的相关文章

[HTML5 Canvas学习]绘制矩形

1.使用strokeRect和fillRect方法绘制矩形 a.strokeRect是绘制一个不填充的矩形 b.fillRect是绘制一个填充的矩形 代码: <script> var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'); context.lineJoin = 'round'; context.lineWidth = 20; context.strokeRect(10, 10,

HTML5 canvas图片爆炸特效

这是一款基于html5 canvas的炫酷图片爆炸飞散特效js插件.该js插件当用鼠标点击图片时,图片会有玻璃窗被子弹击碎时的爆炸飞散效果,非常炫酷. 在线演示:http://www.htmleaf.com/Demo/201502151384.html 下载地址:http://www.htmleaf.com/html5/html5-canvas/201502151383.html

HTML5 Canvas ( 图片填充样式 ) fillStyle, createPattern

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

html5 canvas 鼠标绘制

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> body{ background:pink; } #c1{ background:white; } </style> <script> window.onload = function () { var oC = do

html5 canvas图片渐变

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload = function () { var oc = document.getElementById('c1'); var ogc = oc.getContext('2d'); var yimg = new Imag

html5 canvas图片翻转

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload = function () { var oc = document.getElementById('c1'); var ogc = oc.getContext('2d'); var yimg = new Imag

html5 canvas图片反色

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload = function () { var oc = document.getElementById('c1'); var ogc = oc.getContext('2d'); var yimg = new Imag

html5 canvas图片马赛克

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload = function (){ var oc = document.getElementById('c1'); var ogc = oc.getContext('2d'); var yimg = new Image

html5 canvas路径绘制2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-