小程序-涂鸦画笔(案例-集福)

不同的项目总是要求不同的需求   在下面的代码中,我自码了画福的canvas代码,及橡皮檫的代码;

下面的图中也需实现背景图的点击切换取,点击画图完毕按钮,就实现了内容的提交

wxml:

<canvas  canvas-id="myCanvas"  class="myCanvas" style="width:{{Cwidth1}}px;height:{{Cheight1}}px;top:{{Ctop1}}px;left:{{Cleft1}}px

      disable-scroll="false"

      bindtouchstart="touchStart"

      bindtouchmove="touchMove"

      bindtouchend="touchEnd">

</canvas>

 <view class="box box5" bindtap="clearCanvas"></view>     //橡皮檫按钮

js:

var canvasC ;   //声明一个全局变量,用于橡皮檫

Page({

onLoad: function () {
      var that = this;
     wx.getSystemInfo({
            success: function (res) {
                W = res.windowWidth;
                H = res.windowHeight;
                that.setData({
                    Cwidth: W,
                    Cheight: H / 1.7,
                    Cwidth1: W,
                    Cheight1: H / 1.7,
                    Ctop1: 0,
                    Cleft1: 0
                })
             },
     })
},

 startX : 0;   //保存X坐标轴变量

 startY : 0;   //保存Y坐标轴变量 

//手指触摸动作开始

touchStart:function(e){

      //得到触摸点的坐标

    this.startX = e.changedTouches[0].x

    this.startY = e.changedTouches[0].y

    this.context = wx.createCanvasContext("myCanvas")

    canvasC = this.context;    //将画布保存进全局变量

    新添2018-3-6

    this.context.setStrokeStyle("#ff0000")    //设置笔触颜色

    this.context.setLineWidth(10)    //设置笔触粗细

    this.context.setLineCap("round")   //设置笔触线条圆润

    this.context.beginPath()

}

//手指触摸后移动

touchMove:function(e){

      var startX1 = e.changedTouches[0].x

      var startY1 = e.changedTouches[0].y

     this.context.moveTo(this.startX,this.startY)

      this.context.lineTo(startX1,startY1)

      this.context.stroke()

      this.startX = startX1;

      this.startY = startY1;

      wx.drawCanvas({

            canvasId:"myCanvas",

            reserve:true,

            actions:this.context.getActions()   //获取绘图动作数组

      })

},

// 点击橡皮檫全部清除

clearCanvas:function(){

      canvasC.clearRect(0,0,w,H/1.7);

      canvasC.draw()

}

 })

转载于:https://www.cnblogs.com/liuqingxia/p/8440618.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第02个小程序:遍历画笔(FlipThroughTheBrushes.cs) using System; using System.Reflection; using System.Windows; using System.Windows.Input; using System.Windows.Media; namespace Chapter02 { public class FlipThroughTheBrushes : Window { int index = 0; //索引值 PropertyInfo[] props; //属性数组 [STAThread] public static void Main() { Application app = new Application(); app.Run(new FlipThroughTheBrushes()); } public FlipThroughTheBrushes() { //取得Brushes类的成员,放在props中 props = typeof(Brushes).GetProperties(BindingFlags.Public | BindingFlags.Static); SetTitleAndBackground(); } //鼠标按下 protected override void OnKeyDown(KeyEventArgs args) { if (args.Key == Key.Down || args.Key == Key.Up) { //Key.Downindex-1,Key.UPindex+1 index += args.Key == Key.Up ? 1 : props.Length - 1; //index对属性数组长度取余,防止索引越界 index %= props.Length; SetTitleAndBackground(); } base.OnKeyDown(args); } //设置背景 void SetTitleAndBackground() { Title = "Flip Through the Brushes - " + props[index].Name; Background = (Brush)props[index].GetValue(null, null); //设置背景画笔 } } } GetProperties()函数用于返回对象数组,参数是限制Brushes公开和静态的属性。其实这里可以不需要这样的限制,因为Brushes的属性本来全部都是public和static。 props[index].Name返回第index个属性的名称;props[index].GetValue(null, null)返回实际的SolidColorBrush对象,第一个参数是属性所在的对象,因为Brushes是一个静态属性,没有对应的对象,传入null;第二个参数只有在属性是索引器是才有必要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值