canvas 订阅
《canvas》是从2005年10月3日开始放映的24集日本动画,由F&C ZEXCS制作发行,讲述了拥有超人的绘画天分的男主角的故事。 展开全文
《canvas》是从2005年10月3日开始放映的24集日本动画,由F&C ZEXCS制作发行,讲述了拥有超人的绘画天分的男主角的故事。
信息
外文名
Canvas
制作·发行
F&C ZEXCS
放映日期
2005年10月3日
日文名
虹色のスケッチ
国    家
日本
总共集数
24
canvas剧情简介
拥有超人的绘画天分的男主角,却因某事故无法再次执起画笔,只能委身于某所大学,担任美术课的顾问。受男主角指导走向绘画之路的女主角,却因目睹双亲车祸身亡的现场,而在作画中无法使用红色。青梅竹马的女伴,可是高中时代的告白,却遭到男主角的拒绝,现在她又重新闯入了男主角的生活。 偶然的再会…… 三人之间,会发生什么?爱的前路,会走向哪里?
收起全文
精华内容
参与话题
问答
  • canvas

    千次阅读 多人点赞 2018-04-17 15:29:27
    canvas画布的左上角为笛卡尔坐标系的原点,且y轴的正方向向下,x轴的正方向向右 二、准备工作:布置画布、获取画布、获取画笔 <canvas id=“canvas”></canvas> var canvas = document....

    一、规则

    以canvas画布的左上角为笛卡尔坐标系的原点,且y轴的正方向向下,x轴的正方向向右

    二、准备工作:布置画布、获取画布、获取画笔

    <canvas id=“canvas”></canvas>
    var canvas = document.getElementById("canvas")
    var context = canvas.getContext("2d")

    三、分类

    3、1:线条

    移动画笔:context.moveTo(100,100),画笔起点

    画笔停点:context.linrTo(600,600),画笔终点

    画笔粗细的配置:context.lineWidth='5'

    画笔颜色的配置:context.strokeStyle="#353537"

    绘制类型:fill()填充  、stroke()描边

    beginpath()开始绘制,可以使后面的线段的颜色等不覆盖前面的,结束绘制为fill()、stroke()、closePath()

    	var canvas = document.getElementById('canvas');
            var context = canvas.getContext('2d');
            context.beginPath();
            context.moveTo(20,20);
            context.lineTo(30,30);
            context.lineTo(20,40);
            context.lineTo(20,20);
            context.closePath();//不使用closePath结束绘制,左上角的会有缺口,用closePath的时候可以不用进行结尾,即最后一笔可以不画
            context.lineWidth = '2'; //线条粗细
            context.strokeStyle = '	#228B22';//线条颜色
            context.fillStyle = "yellow";//内容填充色
            context.stroke();//描边结束
            context.fill(); //绘制结束
    3、2:矩形,结合上面的知识点,矩形直接上例子就可以了
         window.onload = function(){
                var canvas = document.getElementById("canvas");
                var context = canvas.getContext("2d");
    
                rectfun(context,80,80,100,100,2,'black','white');
                rectfun(context,85,85,90,90,2,'black','white');
                rectfun(context,87,87,85,85,1,'black','white');
                rectfun(context,90,90,80,80,2,'black','white');
                rectfun(context,100,100,60,60,2,'black','white');
                rectfun(context,110,110,40,40,2,'black','white');
                rectfun(context,120,120,20,20,2,'black','white');
                rectfun(context,130,130,1,1,2,'black','white');
         }
       function rectfun(cxt,x,y,width,height,borderwidth,bordercolor,fillcolor){
                cxt.beginPath();
                cxt.rect(x,y,width,height);//生成矩形
                // cxt.closePath() 使用rect的时候,closePath可以不用
                cxt.lineWidth = borderwidth;//宽度
                cxt.strokeStyle = bordercolor;//颜色
                cxt.fillStyle = fillcolor;//填充色
                cxt.stroke();
                cxt.fill();
         }矩形效果图如下:

    
     

    3、3:线条属性,共有4个属性

          3.3.1、lineCap属性(线条的帽子):定义线的终点属性,有3个属性值(这些属性只会在终点起作用,折线处不会起作用)

    * butt:默认值,端点是垂直于线段边缘的平直边缘。
    * round:端点是在线段边缘处以线宽为直径的半圆。
    * square:端点是在选段边缘处以线宽为长、以一半线宽为宽的矩形。

        例子:

        window.onload = function(){
            var canvas = document.getElementById("canvas");
            canvas.width = 800;
            canvas.height = 600;
            var context = canvas.getContext("2d");
    
            context.lineWidth = 50;
            context.strokeStyle = "#1BAAAA";
    
            context.beginPath();
            context.moveTo(100,100);
            context.lineTo(700,100);
            context.lineCap = "butt";
            context.stroke();
    
            context.beginPath();
            context.moveTo(100,300);
            context.lineTo(700,300);
            context.lineCap = "round";
            context.stroke();
    
            context.beginPath();
            context.moveTo(100,500);
            context.lineTo(700,500);
            context.lineCap = "square";
            context.stroke();
    
            //下面画两个基准线方便观察
            context.lineWidth = 3;
            context.strokeStyle = "black";
    
            context.beginPath();
            context.moveTo(100,0);
            context.lineTo(100,600);
            context.moveTo(700,0);
            context.lineTo(700,600);
            context.stroke();
        }

        效果:

        3.3.2、lineJoin(线条的连接),定义折线处的属性,直接看代码和效果就可以明白

        代码:

    window.onload = function(){
            var canvas = document.getElementById("canvas");
            canvas.width = 800;
            canvas.height = 600;
            var context = canvas.getContext("2d");
    
            context.beginPath();
            context.moveTo(100,100);
            context.lineTo(300,300);
            context.lineTo(100,500);
            context.lineJoin = "miter";
            context.lineWidth = 20;
            context.strokeStyle = "red";
            context.stroke();
    
            context.beginPath();
            context.moveTo(300,100);
            context.lineTo(500,300);
            context.lineTo(300,500);
            context.lineJoin = "bevel";
            context.lineWidth = 20;
            context.strokeStyle = "blue";
            context.stroke();
    
            context.beginPath();
            context.moveTo(500,100);
            context.lineTo(700,300);
            context.lineTo(500,500);
            context.lineJoin = "round";
            context.lineWidth = 20;
            context.strokeStyle = "black";
            context.stroke();
        }

        效果:

        

            3.3.3、当lineJoin设置为miter时(默认),此时可以使用miterLimit属性(不常用)

            例子:

            

    window.onload = function(){
            var canvas = document.getElementById("canvas");
            canvas.width = 800;
            canvas.height = 600;
            var context = canvas.getContext("2d");
    
            context.beginPath();
            context.moveTo(100,100);
            context.lineTo(300,300);
            context.lineTo(100,500);
            context.lineJoin = "miter";
            context.miterLimit = 10;
            context.lineWidth = 5;
            context.strokeStyle = "red";
            context.stroke();
    
            context.beginPath();
            context.moveTo(300,200);
            context.lineTo(500,300);
            context.lineTo(300,400);
            context.lineJoin = "miter";
            context.miterLimit = 10;
            context.lineWidth = 5;
            context.strokeStyle = "blue";
            context.stroke();
    
            context.beginPath();
            context.moveTo(500,290);
            context.lineTo(700,300);
            context.lineTo(500,310);
            context.lineJoin = "miter";
            context.miterLimit = 10;
            context.lineWidth = 5;
            context.strokeStyle = "black";
            context.stroke();
        }

    效果:原理链接:http://7xkcl8.com1.z0.glb.clouddn.com/edu6-4.png

    3、4填充颜色

        3.4.1填充基本颜色

        (1)颜色字符串填充

    context.fillStyle = "red";

        (2)十六进制字符串

    context.fillStyle = "#FF0000";

        (3)rgb

    context.fillStyle = "rgba(255,0,0,1)";

        (4)hsl()

    context.fillStyle = "hsl(0,100%,50%)";

        (5)hsla()

    context.fillStyle = "hsla(0,100%,50%,1)";

    3、5填充渐变形状,线性和径向

        3.5.1线性渐变

            例子:

    var canvas = document.getElementById("canvas");
            canvas.width = 800;
            canvas.height = 600;
            var context = canvas.getContext("2d");
    
            context.rect(200,100,400,400);//x起始坐标  y起始坐标  宽  高 
    
            //添加渐变线
            var grd = context.createLinearGradient(200,300,600,300);  //xstart,ystart,xend,yend 从(200,300)到(600,300)的径向
    
            //添加颜色断点
            grd.addColorStop(0,"black");
            grd.addColorStop(0.5,"white");  
            grd.addColorStop(1,"black");
    
            //应用渐变
            context.fillStyle = grd;
    
            context.fill();

            效果:

            

            3.5.2绘制矩形的快捷方式

    `fillRect(x,y,width,height)`、`stroke(x,y,width,height)`。这两个函数可以分别看做`rect()`与`fill()`以及`rect()`与`stroke()`的组合。因为`rect()`仅仅只是规划路径而已,而这两个方法确实实实在在的绘制。

        例子,直接看例子就可以直接看明白:

    window.onload = function(){
            var canvas = document.getElementById("canvas");
            canvas.width = 800;
            canvas.height = 600;
            var context = canvas.getContext("2d");
            //添加渐变线
            var grd = context.createLinearGradient(100,300,700,300);xstart,ystart,xend,yend 从(100,300)到(700,300)的径向
            //添加颜色断点
            grd.addColorStop(0,"olive");
            grd.addColorStop(0.5,"aqua");
            grd.addColorStop(0.75,"fuchsia");
            grd.addColorStop(0.25,"teal");
            //应用渐变
            context.fillStyle = grd;
            context.strokeStyle = grd;
    
            context.strokeRect(200,150,450,50);
    
            context.fillRect(200,300,300,50);
        }

        效果:

        3、6:填充样式,createPattern()填充图案,需要传递两个参数createPattern(img,repeat-style),第一个对象是img实例,第二个对象是string类型,有4种填充类型,repeat,repeat-x,repeat-y,no-repeat,第一种参数还可以传入一个canvas或者是video对象,这里只说img对象,其余的自己尝试

    window.onload = function(){
            var canvas = document.getElementById("canvas");
            canvas.width = 800;
            canvas.height = 600;
            var context = canvas.getContext("2d");
    
            var img = new Image();//创建img对象
            img.src = "1.jpg";//为img对象指定图片源
            img.onload = function(){
                var pattern = context.createPattern(img, "repeat");//指定类型
                context.fillStyle = pattern;//纹理填充
                context.fillRect(0,0,800,600);//快速制作矩形的方法,xstart,ystrart,width,height
       }

        效果图:(代码里面img使用onload,是对图片进行预加载,)

        3、7:绘制标准圆弧arc()函数

            arc(x,y,r,startAngle,endAngle,anticlockwise)

            x,y圆心坐标,r半径,   

            startAngle:开始的弧度值,endAngle:结束的弧度值

            anticlockwise:表示绘制的方法(用布尔值进行表示),是(false)顺时针还是(true)逆时针,当此值不填写的时候,默认为false,顺时针,弧度值得规则如图:

            

        例子:(我画的准备图的是随便在浏览器的页面上画的,请自动忽略无用的背景)

            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");
            context.fillStyle = "#FFF";
            context.fillRect(0,0,800,600);
    
            context.beginPath();
            context.arc(150,250,50,Math.PI, Math.PI * 3 / 2);
            context.lineTo(250,200);
            context.arc(250,250,50,Math.PI* 3 / 2, Math.PI *2);
            context.lineTo(300,400);
            context.arc(250,400,50,0, Math.PI*1/2);
            context.lineTo(150,450);
            context.arc(150,400,50,Math.PI*1/2, Math.PI);
            // context.lineTo(100,250);
            context.closePath();
            context.strokeStyle = "#0078AA";
            context.stroke();

        (准备图)

    (效果图)

    3、8:切点绘制圆弧arcTo()

    。。。

    展开全文
  • Canvas

    千次阅读 2017-06-22 15:45:04
    canvas.drawLine(0, 0, 100, 100, paint);画多条线: float []pts = {0,0,100,100,200,200,300,300}; canvas.drawLines(pts, paint);// 画虚线–也通过多条线的方式 画点画一个点: canvas.drawPoint(500, 500, ...

    画线

    画一条直线:
    canvas.drawLine(0, 0, 100, 100, paint);

    画多条线:
    float []pts = {0,0,100,100,200,200,300,300};
    canvas.drawLines(pts, paint);// 画虚线–也通过多条线的方式
    这里写图片描述

    画点

    画一个点:
    canvas.drawPoint(500, 500, paint);

    画多个点:
    float []pts = {0,0,100,100,200,200,300,300};
    canvas.drawPoints(pts, paint);
    这里写图片描述

    画矩形

    RectF r = new RectF(100, 100, 400, 500);
    canvas.drawRect(r, paint);

    canvas.drawRect(left, top, right, bottom, paint);

    画圆角矩形

    RectF r = new RectF(100, 100, 200, 200);
    //x-radius ,y-radius圆角的半径
    canvas.drawRoundRect(r, 10, 10, paint);

    画圆

    canvas.drawCircle(300, 300, 200, paint);

    画椭圆

    RectF r = new RectF(100, 100, 300, 200);
    canvas.drawOval(r, paint);

    画圆弧

    canvas.drawArc(
    rect,
    startAngle, //其实角度,相对X轴正方向
    sweepAngle, //画多少角度的弧度
    useCenter, //boolean, false:只有一个纯弧线;true:闭合的边
    paint);

    RectF r = new RectF(100, 100, 400, 400);
    canvas.drawArc(r, 0, 90, true, paint);//顺时针旋转90度

    true:
    这里写图片描述
    false:
    这里写图片描述

    Path

    这里写图片描述

    rLineTo,rMoveTo 带r开头基于前一个点的相对位置
    LineTo,MovoTo 不带r开头的,传的是我们的绝对位置

    Path path = new Path();
    //移动
    path.moveTo(100, 300);//画笔落笔的位置
    //连线(100, 300)—(200, 100)
    path.lineTo(200, 100);
    这里写图片描述
    //连线(200, 100) — (200, 200)
    path.lineTo(200, 200);
    这里写图片描述
    //拼接
    path.cubicTo(250, 200, 350, 300, 450, 400);//连接(200, 200)
    这里写图片描述
    //首尾相接
    path.close();
    canvas.drawPath(path, paint);
    这里写图片描述

    圆角矩形路径

    RectF r = new RectF(100, 100, 200, 200);
    Path path = new Path();
    float radii[] = {10,10,50,50,0,0,10,10};
    path.addRoundRect(r, radii, Path.Direction.CW);
    canvas.drawPath(path, paint);

    这里写图片描述

    RectF r = new RectF(100, 100, 200, 200);
    Path path = new Path();
    path.addArc(r, 0, 90);
    canvas.drawPath(path, paint);

    这里写图片描述

    贝塞尔曲线

            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(4);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.BLACK);
            mStartX = 60;
            mStartY = 350;
            mEndX = 450;
            mEndY = 350;
            mPath = new Path();
            mPath.reset();
            mPath.moveTo(mStartX, mStartY);
            mPath.quadTo(mContorlX, mContorlY, mEndX, mEndY);
            //mPath.addCircle(); 里面有一个方向值 --顺时针和逆时针
            // canvas.drawTextOnPath();---可以去看一下文字的绘制方向
            canvas.drawPath(mPath, mPaint);
            canvas.drawPoint(mContorlX,mContorlY,mPaint);

    这里写图片描述

    setFillType

            mPaint = new Paint();
            mPaint.setColor(Color.RED);
            mPaint.setStrokeWidth(2);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setAntiAlias(true);

    Path.FillType.WINDING

    WINDING 模式 — 取Path所有所在的区域 – 默认的模式

        private void drawWindingType(Canvas canvas) {
            mPath = new Path();
            mPath.offset(100,100);
            mPath.addCircle(200, 200, 100, Path.Direction.CW);
            mPath.addCircle(300, 300, 100, Path.Direction.CW);
            mPath.setFillType(Path.FillType.WINDING);
            mPaint.setColor(Color.RED);
            canvas.drawPath(mPath,mPaint);
        }

    这里写图片描述

    Path.FillType.EVEN_ODD

    EVEN_ODD 模式 — 取Path所在不相交的区域

        private void drawEvenOddType(Canvas canvas) {
            mPath = new Path();
            mPath.offset(100,100);
            mPath.addCircle(200, 200, 100, Path.Direction.CW);
            mPath.addCircle(300, 300, 100, Path.Direction.CW);
            mPath.setFillType(Path.FillType.EVEN_ODD);
            mPaint.setColor(Color.RED);
            canvas.drawPath(mPath,mPaint);
        }

    这里写图片描述

    Path.FillType.INVERSE_WINDING

    INVERSE_WINDING 模式 – 取path所有未占的区域

        private void drawInverseWinding(Canvas canvas) {
            mPath = new Path();
            mPath.offset(100,100);
            mPath.addCircle(200, 200, 100, Path.Direction.CW);
            mPath.addCircle(300, 300, 100, Path.Direction.CW);
            mPath.setFillType(Path.FillType.INVERSE_WINDING);
            mPaint.setColor(Color.RED);
            canvas.drawPath(mPath,mPaint);
        }

    这里写图片描述

    Path.FillType.INVERSE_EVEN_ODD

    INVERSE_EVEN_ODD 模式 — 取path所有未占和相交的区域

        private void drawInverseEvenOdd(Canvas canvas) {
            mPath = new Path();
            mPath.offset(100,100);
            mPath.addCircle(200, 200, 100, Path.Direction.CW);
            mPath.addCircle(300, 300, 100, Path.Direction.CW);
            mPath.setFillType(Path.FillType.INVERSE_EVEN_ODD);
            mPaint.setColor(Color.RED);
            canvas.drawPath(mPath,mPaint);
        }

    这里写图片描述

    op(Path path, Op op)

            mPaint = new Paint();
            mPaint.setColor(Color.RED);
            mPaint.setStrokeWidth(8);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setAntiAlias(true);

    Path.Op.DIFFERENCE

    DIFFERENCE – 减去Path2后Path1区域剩下的部分

        private void drawDifferenceOp(Canvas canvas) {
            Path path1 = new Path();
            path1.addCircle(150, 150, 100, Path.Direction.CW);
            Path path2 = new Path();
            path2.addCircle(200, 200, 100, Path.Direction.CW);
            path1.op(path2, Path.Op.DIFFERENCE);
            canvas.drawPath(path1, mPaint);
            mPaint.setColor(Color.DKGRAY);
            mPaint.setStrokeWidth(2);
            canvas.drawCircle(150, 150, 100,mPaint);
            canvas.drawCircle(200, 200, 100,mPaint);
        }

    这里写图片描述

    Path.Op.INTERSECT

    INTERSECT — 保留Path2 和 Path1 共同的部分

        private void drawIntersectOp(Canvas canvas) {
            Path path1 = new Path();
            path1.addCircle(150, 150, 100, Path.Direction.CW);
            Path path2 = new Path();
            path2.addCircle(200, 200, 100, Path.Direction.CW);
            path1.op(path2, Path.Op.INTERSECT);
            canvas.drawPath(path1, mPaint);
            mPaint.setColor(Color.DKGRAY);
            mPaint.setStrokeWidth(2);
            canvas.drawCircle(150, 150, 100,mPaint);
            canvas.drawCircle(200, 200, 100,mPaint);
        }

    这里写图片描述

    Path.Op.UNION

    UNION – 保留Path1 和 Path 2

        private void drawUnionOp(Canvas canvas) {
            Path path1 = new Path();
            path1.addCircle(150, 150, 100, Path.Direction.CW);
            Path path2 = new Path();
            path2.addCircle(200, 200, 100, Path.Direction.CW);
            path1.op(path2, Path.Op.UNION);
            canvas.drawPath(path1, mPaint);
            mPaint.setColor(Color.DKGRAY);
            mPaint.setStrokeWidth(2);
            canvas.drawCircle(150, 150, 100,mPaint);
            canvas.drawCircle(200, 200, 100,mPaint);
        }

    这里写图片描述

    Path.Op.XOR

    XOR — 保留Path1 和 Path2 还有共同的部分

        private void drawXorOp(Canvas canvas) {
            Path path1 = new Path();
            path1.addCircle(150, 150, 100, Path.Direction.CW);
            Path path2 = new Path();
            path2.addCircle(200, 200, 100, Path.Direction.CW);
            path1.op(path2, Path.Op.XOR);
            canvas.drawPath(path1, mPaint);
            mPaint.setColor(Color.DKGRAY);
            mPaint.setStrokeWidth(2);
            canvas.drawCircle(150, 150, 100,mPaint);
            canvas.drawCircle(200, 200, 100,mPaint);
        }

    这里写图片描述

    Path.Op.REVERSE_DIFFERENCE

    REVERSE_DIFFERENCE — 减去Path1后Path2区域剩下的部分

        private void drawReverseDifferenceOp(Canvas canvas) {
            Path path1 = new Path();
            path1.addCircle(150, 150, 100, Path.Direction.CW);
            Path path2 = new Path();
            path2.addCircle(200, 200, 100, Path.Direction.CW);
            path1.op(path2, Path.Op.REVERSE_DIFFERENCE);
            canvas.drawPath(path1, mPaint);
            mPaint.setColor(Color.DKGRAY);
            mPaint.setStrokeWidth(2);
            canvas.drawCircle(150, 150, 100,mPaint);
            canvas.drawCircle(200, 200, 100,mPaint);
        }

    这里写图片描述

    Region

            RectF r = new RectF(100, 100, 200, 300);
            Path path = new Path();
            path.addOval(r, Path.Direction.CCW);
    
            //创建一块矩形的区域
            Region region = new Region(100, 100, 200, 250);
            Region region1 = new Region();
            region1.setPath(path, region);//path的椭圆区域和矩形区域进行交集
    
            //结合区域迭代器使用(得到图形里面的所有的矩形区域)
            RegionIterator iterator = new RegionIterator(region1);
    
            Rect rect = new Rect();
            while (iterator.next(rect)) {
                canvas.drawRect(rect, paint);
            }

    这里写图片描述

    合并union:

            //合并
            Region region = new Region(50, 50, 100, 250);
            Rect r = new Rect(30, 30, 200, 100);
    //        canvas.drawRect(r, paint);
    //        paint.setColor(Color.BLUE);
            region.union(r);
    //        region.op(r, Region.Op.INTERSECT);//交集部分 region是调用者A,r是求交集的B
            RegionIterator iterator = new RegionIterator(region);
    
            Rect rect = new Rect();
            while (iterator.next(rect)) {
                canvas.drawRect(rect, paint);
            }

    这里写图片描述

    求交集:

            Region region = new Region(50, 50, 100, 250);
            Rect r = new Rect(30, 30, 200, 100);
    //        canvas.drawRect(r, paint);
    //        paint.setColor(Color.BLUE);
    //        region.union(r);
            region.op(r, Region.Op.INTERSECT);//交集部分 region是调用者A,r是求交集的B
            RegionIterator iterator = new RegionIterator(region);
    
            Rect rect = new Rect();
            while (iterator.next(rect)) {
                canvas.drawRect(rect, paint);
            }

    这里写图片描述

    Canvas变换

    平移

    canvas.translate(150, 50);

            // 绘制坐标系
            // 第一次绘制坐标轴
            paint.setColor(Color.GREEN);
            canvas.drawLine(0,0,canvas.getWidth(),0,paint);// X 轴
            paint.setColor(Color.BLUE);
            canvas.drawLine(0,0,0,canvas.getHeight(),paint);// Y 轴
    
            //平移--即改变坐标原点
            canvas.translate(150, 50);
            // 第二次绘制坐标轴
            paint.setColor(Color.GREEN);
            canvas.drawLine(0,0,canvas.getWidth(),0,paint);// X 轴
            paint.setColor(Color.BLUE);
            canvas.drawLine(0,0,0,canvas.getHeight(),paint);// Y 轴

    这里写图片描述

    缩放

    //sx,sy:分别对x/y方向的一个缩放系数,画布的缩放会导致里面所有的绘制的东西都会有一个缩放效果
    canvas.scale(1.5f, 0.5f);

            RectF r = new RectF(0, 0, 400, 500);
            canvas.drawRect(r, paint);
            paint.setColor(Color.BLUE);
            //sx,sy:分别对x/y方向的一个缩放系数,画布的缩放会导致里面所有的绘制的东西都会有一个缩放效果
            canvas.scale(1.5f, 0.5f);
            canvas.drawRect(r, paint);

    这里写图片描述

    旋转

    canvas.rotate(30);

            // 绘制坐标系
            // 第一次绘制坐标轴
            paint.setColor(Color.GREEN);
            canvas.drawLine(0,0,canvas.getWidth(),0,paint);// X 轴
            paint.setColor(Color.BLUE);
            canvas.drawLine(0,0,0,canvas.getHeight(),paint);// Y 轴
    
            //旋转
            canvas.rotate(30);
            // 第二次绘制坐标轴
            paint.setColor(Color.GREEN);
            canvas.drawLine(0,0,canvas.getWidth(),0,paint);// X 轴
            paint.setColor(Color.BLUE);
            canvas.drawLine(0,0,0,canvas.getHeight(),paint);// Y 轴

    这里写图片描述

    斜拉

    //sx,sy倾斜度:X轴方向上倾斜60度,tan60=根号3
    canvas.skew(1.73f, 0);

            RectF r = new RectF(100, 100, 200, 200);
            canvas.drawRect(r, paint);
            paint.setColor(Color.BLUE);
            //sx,sy倾斜度:X轴方向上倾斜60度,tan60=根号3
            canvas.skew(1.73f, 0);
            canvas.drawRect(r, paint);

    这里写图片描述

    裁剪

    canvas.clipRect(new Rect(250, 250, 300, 400));

            RectF r = new RectF(200, 200, 400, 500);
            canvas.drawRect(r, paint);
            paint.setColor(Color.BLUE);
            canvas.clipRect(new Rect(250, 250, 300, 400));
            canvas.drawBitmap(mBitmap, 250, 250, paint);
            paint.setColor(Color.GREEN);

    这里写图片描述

    save和restore、restoreToCount

        private void saveRestore(Canvas canvas, Paint paint) {
            canvas.translate(50, 50);
            RectF r = new RectF(0, 0, 200, 100);
            paint.setColor(Color.GREEN);
            canvas.drawRect(r, paint);
            canvas.save();
            //平移
            canvas.translate(50, 50);
    
    
            paint.setColor(Color.BLUE);
            canvas.drawRect(r, paint);
    
            canvas.restore();
            //canvas.rotate(45);
    
            paint.setColor(Color.YELLOW);
            r = new RectF(0, 0, 100, 200);
    
            canvas.drawRect(r, paint);
        }

    这里写图片描述

            Paint paint = new Paint();
            RectF r = new RectF(50, 50, 150, 150);
            //当canvas执行saveLayer的时候就会新建一个新的透明画布图层
            int layer = canvas.saveLayer(0,0,canvas.getWidth(),canvas.getHeight(),null,Canvas.ALL_SAVE_FLAG);
    
            canvas.drawColor(Color.YELLOW);
            canvas.translate(50, 50);
            paint.setColor(Color.BLUE);
            canvas.drawRect(r, paint);
    
            canvas.restoreToCount(layer);
    
    
            RectF rectF = new RectF(0,0,100,100);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(10);
            paint.setColor(Color.GREEN);
    
            canvas.drawRect(rectF, paint);
            canvas.translate(50,50);

    这里写图片描述

            Paint paint = new Paint();
            RectF rectF = new RectF(0,0,100,100);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(10);
            paint.setColor(Color.GREEN);
    
            canvas.drawRect(rectF, paint);
            canvas.translate(50,50);
    
            int layerid = canvas.saveLayer(0,0,canvas.getWidth(),canvas.getHeight(),null,Canvas.ALL_SAVE_FLAG);
            //canvas.save();
            canvas.drawColor(Color.BLUE);// 通过drawColor可以发现saveLayer是新建了一个图层,
            // 同时结合Lsn5的16种Xfermode叠加形式Demo可以验证是新建的透明图层
            paint.setColor(Color.YELLOW);
            canvas.drawRect(rectF,paint);
    //        canvas.restore();
            canvas.restoreToCount(layerid);
    
            RectF rectF1 = new RectF(10,10,300,400);
            paint.setColor(Color.RED);
            canvas.drawRect(rectF1,paint);

    这里写图片描述

            //save restoreToCount
            mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            Paint mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(10);
            canvas.save();//1
    
            canvas.translate(200, 200);
            mPaint.setColor(Color.RED);
            RectF rectF = new RectF(0,0,200,200);
    
            canvas.drawRect(rectF,mPaint);
    
            canvas.save();//2
    
            canvas.rotate(45);
            mPaint.setColor(Color.BLUE);
            canvas.drawRect(rectF,mPaint);
    
            canvas.save();//3
    
            canvas.rotate(45);
            mPaint.setColor(Color.YELLOW);
            canvas.drawRect(rectF,mPaint);
    
            canvas.save();//4
    
            canvas.restoreToCount(1);
    
            canvas.translate(20, 100);
            mPaint.setColor(Color.GREEN);
            canvas.drawRect(rectF,mPaint);

    这里写图片描述

    展开全文
  • (精华)2020年6月29日 Canvas 刮刮乐

    万次阅读 2020-06-29 21:25:15
    <!... <... <head> ...meta charset="UTF-8">...canvas刮奖游戏</title> <style> *{ margin: 0; padding: 0; } .prize { position: absolute; width: 300px; height: 150px; text-ali
    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>canvas刮奖游戏</title>
    		<style>
    		*{
        margin: 0;
        padding: 0;
    }
    .prize {
        position: absolute;
        width: 300px;
        height: 150px;
        text-align: center;
        line-height: 150px;
        font-size: 30px;
        color: red;
    }
    #canvas{
        border: 1px solid #000;	
        position: absolute;
        z-index: 2;
    }
    
    		</style>
      		<script >
      		window.onload = function(){
    	var flag = false; //是否在拖动中
    	var canvas = document.getElementById("canvas");
    	var ctx = canvas.getContext("2d");  //绘制图形
    
    	//画一个矩形
    	ctx.beginPath(); //开始路径
    	ctx.rect(0,0,300,150);  //绘制矩形    fillRect 实心   strokeRect空心 ,只有边框
    	ctx.fillStyle='#c0c0c0';//填充颜色
    	ctx.fill();   //填充
    	ctx.closePath(); //关闭路径
    	
    	//按下事件
    	canvas.onmousedown = function(event){    //event对象    事件的状态  e.target
    		flag = true;
    	};
    		//移动事件
    		canvas.onmousemove = function(event){
    			if(!flag) {
    				return;
    			}
    			var x = event.clientX;    //与X轴的距离
    			var y = event.clientY;   //与Y轴的距离
    			//清除绘制
    			ctx.clearRect(x,y,20,20);
    		}
    	//松开事件
    	canvas.onmouseup= function(event){    //event对象    事件的状态  e.target
    		flag = false;
    	};
    
    	//中奖信息
    	var arr = ['一个亿','海景别墅','一等奖','二等奖','100元话费','10G流量','谢谢惠顾'];
    	var prize = document.querySelector(".prize");
    	var i = Math.floor(Math.random()*arr.length);  //随机数 parseInt
    	console.log(i);
    	prize.innerText = arr[i];   //0-5
    
    }
    </script>
    	</head>
    	<body>
    		<canvas id="canvas" width="300" height="150"></canvas>
    		<div class="prize">谢谢惠顾</div>
    	</body>
    
    </html>
    
    
    展开全文
  • (精华)2020年6月28日 Canvas 效果动画

    万次阅读 2020-06-28 10:16:48
    渐变 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <...渐变&

    渐变

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <meta http-equiv="X-UA-Compatible" content="ie=edge">
       <title>渐变</title>
    </head>
    
    <body>
       <canvas id="canvas" width="600" height="800"></canvas>
       <script>
          //线性渐变
          // createLinearGradient(0,0,200,0);
          var canvas = document.getElementById('canvas');
          var ctx = canvas.getContext('2d');
          // 创建一个线性渐变。使用渐变填充矩形:
          function drawGradient() {
             //开始一个新的绘制路径
             ctx.beginPath();
             ctx.translate(50, 50);
             //创建渐变圆环
             var grd = ctx.createLinearGradient(0, 0, 200, 0);
             //镜像渐变
             //    createRadialGradient(x,y,r,x1,y1,r1)
             grd.addColorStop(0.5, 'red');
             //    grd.addColorStop(0.5,'green');
             //    grd.addColorStop(0.8,'red');
             grd.addColorStop(1, 'blue'); //填充渐变
             ctx.lineWidth = 8;
             ctx.strokeStyle = grd;
             ctx.fillStyle = grd;
             //沿着 100,100 位圆心,画圆
             ctx.arc(100, 100, 50, 0, 360 * Math.PI / 180, true); //路径
             //    ctx.stroke(); // 圆环
             ctx.fill(); //实心圆
          }
          drawGradient();
       </script>
    </body>
    
    </html>
    

    网格

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>网格</title>
    </head>
    
    <body>
        <canvas id="canvas" width="850" height="750" style="border:1px solid #000"></canvas>
        <script>
            var canvas = document.getElementById('canvas');
            if (canvas.getContext) {
                var ctx = canvas.getContext('2d'); //绘制工具,
                //画网格
                function drawLine(stepx, stepy) {
                    //样式
                    ctx.lineWidth = 0.5;
                    ctx.strokeStyle = 'green';
                    //绘制竖线
                    for (var i = stepx + 0.5; i < canvas.width; i += stepx) {
                        ctx.beginPath(); //新的一条路径
                        ctx.moveTo(i, 0);
                        ctx.lineTo(i, canvas.height);
                        ctx.stroke();
                    }
                    //绘制横线
                    for (var i1 = stepy + 0.5; i1 < canvas.height; i1 += stepy) {
                        ctx.beginPath(); //新的一条路径
                        ctx.moveTo(0, i1);
                        ctx.lineTo(canvas.width, i1);
                        ctx.stroke();
                    }
                }
                drawLine(10, 10);
            }
        </script>
    </body>
    
    </html>
    

    效果

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>效果</title>
    </head>
    
    <body>
      <canvas id="canvas" width="600" height="800" style="border:1px solid red"></canvas>
    
      <script>
        //平移 transform
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        // ctx.fillStyle = 'green';
        // ctx.fillRect(0,0,50,70);
        //原来的是(画布左上角)
        ctx.save(); //保存原来的状态
        ctx.translate(50, 50);
        ctx.fillStyle = 'red';
        ctx.fillRect(0, 0, 150, 70);
        ctx.restore(); //还原状态
        ctx.save();
        //缩放scale
        ctx.translate(250, 250);
        ctx.scale(1.5, 0.5);
        ctx.fillStyle = 'green';
        ctx.fillRect(0, 0, 50, 70);
        ctx.restore();
        ctx.fillStyle = 'red';
        ctx.fillRect(100, 200, 150, 70);
      </script>
    </body>
    
    </html>
    

    笑脸

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>网格</title>
    </head>
    
    <body>
        <canvas id="canvas" width="850" height="750" style="border:1px solid #000"></canvas>
        <script>
            var canvas = document.getElementById('canvas');
            if (canvas.getContext) {
                var ctx = canvas.getContext('2d'); //绘制工具,
                //    ctx.translate(300,60);
                ctx.beginPath();
                ctx.arc(75, 75, 50, 0, 360 * Math.PI / 180, true); //绘制一个圆
                //    true :逆时针
                ctx.moveTo(110, 75);
                ctx.arc(75, 75, 35, 0, 180 * Math.PI / 180, false); //口
                //    false //顺时针
                ctx.moveTo(65, 65);
                ctx.arc(60, 65, 5, 0, 360 * Math.PI / 180, true); //左眼
                ctx.moveTo(95, 65);
                ctx.arc(90, 65, 5, 0, 360 * Math.PI / 180, true); //右眼
                // ctx.moveTo(200,330);
                // ctx.lineTo(230,400);
                ctx.stroke();
            }
        </script>
    </body>
    
    </html>
    
    展开全文
  • (精华)2020年6月28日 Canvas 元素拖动

    万次阅读 2020-06-28 08:26:55
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta ...图片绘
  • (精华)2020年6月28日 Canvas 抽奖轮盘

    万次阅读 2020-06-28 08:07:41
    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <...canvas id="canvas" width="500" height="500" style="border: 1px solid black"></canva
  • (精华)2020年6月28日 Canvas 动态绘图

    万次阅读 2020-06-28 07:59:25
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta ...绘制
  • (精华)2020年6月28日 Canvas 基础知识

    万次阅读 2020-06-28 07:55:38
    Canvas绘制线条 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta ...
  • var img = jsShot.Canvas2Image.convertToImage(canvas, canvasWidth, canvasHeight); let type = $('#sel').val(); //图片类型 let w = $('#imgW').val(); //图片宽度 let h = $('#imgH').val(); //图片高度...
  • 自定义控件之绘图篇(四):canvas变换与操作

    万次阅读 多人点赞 2014-09-05 15:05:36
    前言:前几篇讲解了有关canvas绘图的一些操作,今天更深入一些,讲讲对画布的操作,这篇文章不像前几篇那么容易理解,如果以前没有接触过画布的童鞋可能比较难以理解,为什么会这样。我尽量多画图,让大家更清晰明白...
  • js实现截图并保存图片(html转canvascanvas转image)

    万次阅读 热门讨论 2018-07-23 15:16:43
    js实现截图并保存图片在本地(html转canvascanvas转image) 一、html转canvas 需要的库html2canvas.js和canvas2image.js 话不多说,直接上代码! html &amp;amp;amp;amp;lt;h2&amp;amp;amp;amp...
  • canvas绘图工具

    万次阅读 2019-01-21 15:34:36
    关于canvas绘图,在html页面上太方便了。为什么不用SVG呢?SVG大量的操作DOM元素你会发现网页的内存一下就达到几个G非常恐怖,更别说应用到移动端了。百度取了不少经,什么画板涂鸦只是小把戏缺乏实用性,灵活性。...
  • 1.新建canvas.js // 炫酷渐变色背景粒子线条折线连接canvas动画 export const canvas = (val) => { var requestAnimationFrame = window.requestAnimationFrame || function (callback) { window....
  • Android Canvas绘图描述Android Canvas 方法总结

    万次阅读 多人点赞 2018-04-23 00:17:47
    Android中使用图形处理引擎,2D部分是android SDK内部自己提供,3D部分是用Open GL ES 1.0。今天我们主要要了解的是2D相关的,如果你想看3D的话那么...他们提供了图形处理相关的: Canvas、ColorFilter、Point(点)和...
  • 自定义了一个View,在初始化时该View会先画一部分图形,然后连接服务器,...我在自定义View的类里,设置了一个Canvas变量can,在onDraw(Canvas canvas )方法的最后一行写了“can=canvas",可是得到的can变量却是null。
  • onrendered: function (canvas) { document.body.appendChild(canvas); convertCanvasToImage(document.getElementsByTagName("canvas")); } }); 生成后用 function convertCanvasToImage(canvas) { ...

空空如也

1 2 3 4 5 ... 20
收藏数 81,642
精华内容 32,656
关键字:

canvas