キャンバス関連文書
エフェクト画像表示1つ目はランダムな色とランダムなサイズを組み合わせた効果です 2 つ目は、ランダムなサイズ分散効果を持つランダムな背景画像です (ここで使用した画像はすべて同じなので、異なる画像は表示されません) ケースの完全なコード
親コンポーネントコード<テンプレート> <div id="ホーム"> <div class="タグ" ref="タグ"> <circle-box :parentClientWidth="親クライアントの幅" :parentClientHeight="親クライアントの高さ" :dataList="データリスト"></circle-box> </div> </div> </テンプレート> <スクリプト> '@/components/content/circle/Circle.vue' から CircleBox をインポートします。 エクスポートデフォルト{ コンポーネント: { CircleBox }, データ() { 戻る { 親クライアント幅: 0, 親クライアントの高さ: 0, // キャンバスシミュレーションデータ dataList: [ { フォロー: 1, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 2, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 3, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 4, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 5, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 6, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 7, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 8, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 9, 画像: 'http://39.99.139.115/demo/RB5.png' }, { フォロー: 10, 画像: 'http://39.99.139.115/demo/RB5.png' } ]、 }; }, 作成された() {}, マウント() { this.getWidth(); }, メソッド: { // 親ボックスの幅と高さを取得する getWidth() { this.parentClientWidth = this.$refs.tags.clientWidth; this.parentClientHeight = this.$refs.tags.clientHeight; console.log(this.$refs.tags.clientWidth); } }, }; </スクリプト> サブコンポーネントコード<テンプレート> <div> <canvas id="myCanvas" :width="親クライアントの幅 + 'px'" :height="親クライアントの高さ + 'px'"></canvas> </div> </テンプレート> <スクリプト> エクスポートデフォルト{ // データを受け取る props: ['parentClientWidth', 'parentClientHeight', 'dataList'], データ() { 戻る { dataListコピー: this.dataList } }, 作成された() { this.$nextTick(() => { // this.circleInfo() を初期化する }) }, マウント() {}, メソッド: { サークル情報() { それを = これとする クラス Circle { コンストラクタ(x, y, r, 色) { this.x = x this.y = y r = r です this.c = 色 ? 色: this.getRandomColor() } // ランダムカラー getRandomColor() { r = Math.floor(Math.random() * 100) + 155 とします。 g = Math.floor(Math.random() * 100) + 155 とします。 b = Math.floor(Math.random() * 100) + 155 とします。 `rgb(${r},${g},${b})` を返す } } クラス RandomCircle { コンストラクタ(obj) { this.c = document.getElementById(obj.id) コンソールログ(this.c) this.ctx = this.c.getContext('2d') this.dWidth = this.c.width this.dHeight = this.c.height this.fix = obj.fix || 真 this.minMargin = obj.minMargin || 20 this.minRadius = obj.minRadius || 30 this.radiuArr = obj.radiuArr || [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30] this.total = obj.total || 10 this.circleArray = [] this.circleNumber = 1 } 1つの円を描画します(c, インデックス) { // コンソール.log(c, インデックス) ctx = this.ctx とします。 ctx.beginPath() ctx.strokeStyle = cc ctx.fillStyle = cc // 円を描く ctx.arc(cx, cy, cr, 0, 2 * Math.PI) ctx.ストローク() ctx.fill() // ctx.textAlign = 'center' // ctx.textBaseline = 'middle' // ctx.fillStyle = '黒' // ctx.font = '1rem Microsoft YaHei' // ctx.fillText(that.dataListCopy[index].follow, cx, cy - 10) // 円内のテキスト let img = new Image() img.src = that.dataListCopy[インデックス].image ctx.drawImage(img, cx - cr, cy - cr, cr * 2, cr * 2) this.circleNumber++ } チェック(x, y, r) { 戻り値 !(x + r > this.dWidth || x - r < 0 || y + r > this.dHeight || y - r < 0) } // 新しい円の半径を取得します。主に半径と最も近い円の間の距離を決定します。getR(x, y) { if (this.circleArray.length === 0) は Math.floor(Math.random() * 20 + 20) を返します lenArr = this.circleArray.map((c) => { とします。 xSpan = cx - x とします。 ySpan = cy - yとします Math.floor(Math.sqrt(Math.pow(xSpan, 2) + Math.pow(ySpan, 2))) - cr を返す }) minCircleLen = Math.min(...lenArr) とします。 minC = this.circleArray[lenArr.indexOf(minCircleLen)]とします。 tempR = this.fix ? this.radiuArr[this.circleArray.length] : minCircleLen - this.minMargin とします。 bool = this.fix ? tempR <= minCircleLen - minC.r : tempR >= this.minRadius とします。 bool を返す ? tempR : false } // ランダムに生成された中心を持つ円を生成します。 // 200回連続生成しても半径が適切でない場合は、プロセスを終了します createOneCircle() { x、y、rとします createCircleTimes = 0 とします (真)の間{ サークルタイムを作成++ x = Math.floor(Math.random() * this.dWidth) y = Math.floor(Math.random() * this.dHeight) TR = this.getR(x, y) とします。 もし(!TR){ 続く } それ以外 { r = TR } if (this.check(x, y, r) || createCircleTimes > 200) { 壊す } } this.check(x, y, r) && this.circleArray.push(新しいCircle(x, y, r)) } // 新しい円の生成が 100 回失敗した場合は、スキームを終了します。 // 生成された 100 個のソリューションのいずれも適切でない場合は、プロセスを終了します。 初期化() { n = 0とする (this.circleArray.length < this.total) の間 { this.circleArray = [] i = 0とする (this.circleArray.length < this.total) の間 { this.createOneCircle() 私は++ (i >= 100) の場合 { 壊す } } ++ いいえ (n > 100)の場合{ 壊す } } // 半径に応じて大きい円から小さい円まで描きます。 this.circleArray .sort((a, b) => br - ar) .forEach((c, インデックス) => { this.drawOneCircle(c, インデックス) }) } } // コンソールにログ出力します。 const p = 新しいランダムサークル({ id: 'myCanvas', total: that.dataListCopy.length //構成数量}) p.init() コンソールログ(p) コンソールログ(p.circleArray) } } } </スクリプト> 要約するVue が Canvas を使用してランダムなサイズの重なり合わない円を生成する方法については、これで終わりです。Vue が Canvas を使用してランダムな円を生成する方法の詳細については、123WORDPRESS.COM の以前の記事を検索するか、次の関連記事を引き続き参照してください。今後とも 123WORDPRESS.COM をよろしくお願いいたします。 以下もご興味があるかもしれません:
|
<<: ボタンをクリックした後のCSS読み込み効果を実現する
>>: MySQLでテーブルを作成し、フィールドコメントを追加する方法
目次1. オプションAPIと組み合わせAPIの違い2. セットアップを具体的にどのように使用するので...
グループを作成グループ化は、SELECT ステートメントの GROUP BY 句で設定されます。例:...
プロジェクトのテスト環境データベースのデータが失われてしまったので、記録しておきたいと思います。当時...
IE6 での CSS 背景画像のちらつきバグ (IE6 の背景画像キャッシュの問題) IE6 は、背...
1. VMware Workstation 上の Linux: 1. ソースの更新(オプション) v...
序文この記事では、Docker を使用して、ローカル コンピューターにインストールされている開発スイ...
1. レンダリング 2. ソースコードhtml < 本文 > < div クラス ...
背景:サイトはフロントエンドとバックエンドから分離されています: vue+springbootフロン...
概要前の章では、クエリのフィルター条件について学習しました。MySQL では、like % ワイルド...
ソースコードの例: https://codepen.io/shadeed/pen/03caf6b36...
この記事の警告事項は、ブラウザの互換性とはまったく関係ありません。主に、プロジェクトで遭遇したいくつ...
以前、開発で頻繁に pip ダウンロードを使用する必要がありました。pip ソースを国産ソースに変更...
Magento を頻繁に変更する場合、element.style に遭遇することがあります。 これは...
トークンの有効期限が切れたら、ページを更新します。ページの読み込み時にバックエンドに複数のリクエスト...
コードをコピーコードは次のとおりです。 <a href=# title="ここに表示...