SVG-1

<rect>矩形

<circle>圆

<ellipse>椭圆

<line>直线

<polyline>折线

<polygon>标签用来创建含有不少于三个边的图形

stroke:描边的颜色

写好的一个编辑器:

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title>SVG 编辑器</title>    <style>        #toolbox {            position: absolute;            top: 0;            bottom: 0;            left: 0;            width: 250px;            border-right: 1px solid #CCC;        }

        #toolbox h2 {            margin: 0;            padding: 0;            background: #EEE;            font-size: 16px;            height: 24px;            line-height: 24px;            padding: 5px 10px;        }

        #toolbox form {            padding: 10px;        }

        #canvas {            position: absolute;            left: 260px;            top: 10px;            bottom: 10px;            right: 10px;            box-shadow: 2px 2px 10px rgba(0,0,0,.4);            border-radius: 5px;        }

        label {            display: inline-block;            width: 80px;            text-align: right;        }    </style></head><body>    <div id="toolbox">        <h2>创建</h2>        <form id="create-shape">            <button type="button" create="rect">Rect</button>            <button type="button" create="circle">Circle</button>            <button type="button" create="ellipse">Ellipse</button>            <button type="button" create="line">Line</button>        </form>        <h2>形状</h2>        <form id="shape-attrs">            请先创建图形        </form>        <h2>外观和变换</h2>        <form id="look-and-transform" disabled="disabled">            <p>                <label style="display: inline;">填充</label>                <input id="fill" type="color" value="#ffffff" />            </p>            <p>                <label style="display: inline;">描边</label>                <input id="stroke" type="color" value="#ff0000" />                <input id="strokeWidth" type="range" value="1" />            </p>            <p>                <label>translateX</label>                <input id="translateX" type="range" min="-400" max="400" value="0" />

                <label>translateY</label>                <input id="translateY" type="range" min="-400" max="400" value="0" />

                <label>rotate</label>                <input id="rotate" type="range" min="-180" max="180" value="0" />

                <label>scale</label>                <input id="scale" type="range" min="-1" max="2" step="0.01" value="1" />            </p>        </form>    </div>    <div id="canvas"></div></body><script>    var SVG_NS = ‘http://www.w3.org/2000/svg‘;

    // 图形及对应默认属性    var shapeInfo = {        rect: ‘x:10,y:10,width:200,height:100,rx:0,ry:0‘,        circle: ‘cx:200,cy:200,r:50‘,        ellipse: ‘cx:200,cy:200,rx:80,ry:30‘,        line: ‘x1:10,y1:10,x2:100,y2:100‘    };

    // 默认公共属性    var defaultAttrs = {        fill: ‘#ffffff‘,        stroke: ‘#ff0000‘    };

    var createForm = document.getElementById(‘create-shape‘);    var attrForm = document.getElementById(‘shape-attrs‘);    var lookForm = document.getElementById(‘look-and-transform‘);

    var svg = createSVG();    var selected = null;

    createForm.addEventListener(‘click‘, function(e) {        if (e.target.tagName.toLowerCase() == ‘button‘) {            create(e.target.getAttribute(‘create‘));        }    });

    attrForm.addEventListener(‘input‘, function(e) {        if (e.target.tagName.toLowerCase() != ‘input‘) return;        var handle = e.target;        selected.setAttribute(handle.name, handle.value);    });

    lookForm.addEventListener(‘input‘, function(e) {        if (e.target.tagName.toLowerCase() != ‘input‘) return;        if (!selected) return;        selected.setAttribute(‘fill‘, fill.value);        selected.setAttribute(‘stroke‘, stroke.value);        selected.setAttribute(‘stroke-width‘, strokeWidth.value);        selected.setAttribute(‘transform‘, encodeTranform({            tx: translateX.value,            ty: translateY.value,            scale: scale.value,            rotate: rotate.value        }));    });

    function createSVG() {        var svg = document.createElementNS(SVG_NS, ‘svg‘);        svg.setAttribute(‘width‘, ‘100%‘);        svg.setAttribute(‘height‘, ‘100%‘);        canvas.appendChild(svg);

        svg.addEventListener(‘click‘, function(e) {            if (e.target.tagName.toLowerCase() in shapeInfo) {    //判断图形是否在shapeInfo里                select(e.target);     //选中这个图形            }        });        return svg;    }

    function create(name) {        var shape = document.createElementNS(SVG_NS, name);        svg.appendChild(shape);        select(shape);    }

    function select(shape) {        var attrs = shapeInfo[shape.tagName].split(‘,‘);  //在shapeInfo里拿到该图形的属性用‘,‘隔开        var attr, name, value;

        attrForm.innerHTML = "";

        while(attrs.length) {            attr = attrs.shift().split(‘:‘);            name = attr[0];            value = shape.getAttribute(name) || attr[1];  //是否是才生成的,不是就拿以前的属性            createHandle(shape, name, value);            shape.setAttribute(name, value);        }

        for (name in defaultAttrs) {            value = shape.getAttribute(name) || defaultAttrs[name];            shape.setAttribute(name, value);        }        selected = shape;

        updateLookHandle();    }

    function createHandle(shape, name, value) {

        var label = document.createElement(‘label‘);        label.textContent = name;

        var handle = document.createElement(‘input‘);        handle.setAttribute(‘name‘, name);        handle.setAttribute(‘type‘, ‘range‘);        handle.setAttribute(‘value‘, value);        handle.setAttribute(‘min‘, 0);        handle.setAttribute(‘max‘, 800);

        attrForm.appendChild(label);        attrForm.appendChild(handle);    }

    function updateLookHandle() {        fill.value = selected.getAttribute(‘fill‘);  //selected就是当前选中的图形        stroke.value = selected.getAttribute(‘stroke‘);        var t = decodeTransform(selected.getAttribute(‘transform‘));        translateX.value = t ? t.tx : 0;        translateY.value = t ? t.ty : 0;        rotate.value = t ? t.rotate : 0;        scale.value = t ? t.scale : 1;    }

    function decodeTransform(transString) {        var match = /translate\((\d+),(\d+)\)\srotate\((\d+)\)\sscale\((\d+)\)/.exec(transString);        return match ? {            tx: +match[1],            ty: +match[2],            rotate: +match[3],            scale: +match[4]        } : null;    }

    function encodeTranform(transObject) {        return [‘translate(‘, transObject.tx, ‘,‘, transObject.ty, ‘) ‘,            ‘rotate(‘, transObject.rotate, ‘) ‘,            ‘scale(‘, transObject.scale, ‘)‘].join(‘‘);    }

</script></html>
时间: 2024-08-08 01:28:38

SVG-1的相关文章

SVG入门

一. 什么是SVG?SVG(Scalable Vector Graphics)可伸缩矢量图形,它是用XML格式来定义用于网络的基于矢量的图形,而它的特点就是 图像在放大或改变尺寸的情况下其图形质量不会有所损失,同时他和DOM一样都是W3C的一个标准.这里解释下位图和矢量图:位图,也就是我们经常能看到的图片,他是一个平面上密集排布的店的集合,也就是说它是由一个个点构成的.而如果对他进行放大那么相对应的点就会进行放大,这样就会让图片显得十分不清晰粗糙.矢量图,也称为面向对象的图像或绘图图像,在数学上

SVG 文本

该部分为四个主要部分: 1.  <text>和<tspan>标签详解 2.  文本水平垂直居中问题 3.  <textpath>让文本在指定路径上排列  4.  <a>插入超链接 <text>和<tspan>标签 <text>和<tspan>标签是定义文本的基本标签. <text> 参数 描述 默认值 x 文本绘制x轴位置 0 y 文本绘制y轴位置 0 dx 每个字符相对前一个字符的偏移距

svg 路径path

<path> 标签用来定义路径. 下面的命令可用于路径数据: M = moveto(M X,Y) :将画笔移动到指定的坐标位置L = lineto(L X,Y) :画直线到指定的坐标位置H = horizontal lineto(H X):画水平线到指定的X坐标位置V = vertical lineto(V Y):画垂直线到指定的Y坐标位置C = curveto(C X1,Y1,X2,Y2,ENDX,ENDY):三次贝赛曲线S = smooth curveto(S X2,Y2,ENDX,END

SVG矢量图像:微笑

效果图 SVG图片:Happy.svg <?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd"> <svg viewBox="0 0 400

Java Batik操作SVG,实现svg读取,生成,动态操作

SVG在现在的应用场景中还是很常见的,例如绘制复杂的矢量图形.说到SVG,就不得提下Canvas.在这里我就不详细列举它们之间的不同之处,以及为什么要选择SVG或Canvas了. 首先,我的项目是一个Maven项目,所以只需要导入batik的maven依赖就可以了,如果是普通的Java项目,就需要自己找jar包导入项目中了.maven依赖有: <!-- svg 生成png格式图片 --> <dependency> <groupId>batik</groupId&g

SVG渐变

前面的话 给SVG元素应用填充和描边,除了使用纯色外,还可以使用渐变.本文将详细介绍SVG渐变 线性渐变 有两种类型的渐变:线性渐变和径向渐变.必须给渐变内容指定一个id属性,否则文档内的其他元素不能引用它.为了让渐变能被重复使用,渐变内容需要定义在<defs>标签内部,而不是定义在形状上面 线性渐变沿着直线改变颜色,要插入一个线性渐变,需要在SVG文件的defs元素内部,创建一个<linearGradient> 节点 <svg height="70" v

SVG圆形&lt;circle&gt; 标签

1 <?xml version="1.0" standalone="no"?> 2 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 3 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 4 5 <svg width="100%" height="100%" v

svg图转canvas,完全阔以的

遇到的问题:页面中存在svg画的图,也存在canvas图,在用 html2canvas 截取页面的图就导致有图画缺失,至少我需要的缺失了. 一.如果页面单纯的存在一个svg画的图,转为canvas就很好考虑,就是将svg的图画代码转为字符串,去空格,然后用canvg.js的方法, canvg("canvasId","svgHTML")转化,其中canvasId这个对象是你页面的canvas容器(你也可以动态生成它),svgHTML是你拿到的svg 图画的字符串.示例

eclipse禁用svg文件Validation

1.打开window>preferences>validation找到xml validator 2.点击xml validator最右侧的按钮打开xml校验规则窗口,选中exclude group点击右侧的add rule..按钮 3.打开添加规则窗口后,选中第一个file extension点击下一步next,输入框中输入svg,点击完成finish按钮:

SVG.js 图案使用和use引用

一.SVG.Pattern 图案设置 var draw = SVG('svg1').size(300, 300); //SVG.Pattern 图案设置 var pattern = draw.pattern(20, 20, function (add) { add.rect(20, 20).fill('#f06'); add.rect(10, 10); add.rect(10, 10).move(10, 10); }); //获取url的标识对象,returns 'url(#SvgjsPatte