Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app">
  <v-app>
    <v-container>
      <v-layout wrap text-xs-center>
        <v-flex xs12>
          <canvas id="canvas" :width="size.width" :height="size.height" ref="canvas"></canvas>
        </v-flex>
      </v-layout>
      <v-layout wrap justify-center class="mt-3" color="#333">
        <v-flex xs12 sm4 class="pa-2">
          <v-textarea
            v-model="text"
            outline
            hide-details
            label="message"
            rows="3"
            color="#333"
            class="textarea"
            style="margin: auto;"
            @input="changeText"
          ></v-textarea>
        </v-flex>
        <v-flex xs12 sm4 class="pa-2">
          <v-layout>
            <v-flex class="mr-1">
              <v-slider
                v-model="font.size"
                label="font-size"
                color="#333"
                min="10"
                max="150"
                @input="changeFontSize"
              ></v-slider>
            </v-flex>
            <v-flex class="ml-2">
              <v-text-field
                v-model="font.size"
                hide-details
                color="#333"
                type="number"
                suffix="px"
                style="width: 56px;"
                @input="changeFontSize"
              ></v-text-field>
            </v-flex>
          </v-layout>
        </v-flex>
      </v-layout>
    </v-container>
  </v-app>
</div>

              
            
!

CSS

              
                #canvas {
  max-width: 50vw;
  max-height: 280px;
}

.textarea {
  font-size: 18px;
}

              
            
!

JS

              
                new Vue({
  el: '#app',
  
  data() {
    return {
      font: {
        family: "arial",
        size: 40,
        lineHeightOffset: 4
      },
      imgsrc: "http://pictogram2.com/p/p0890/5.png", // 表示画像
      size: { // 表示画像のサイズ
        width: 500,
        height: 500
      },
      textareaRate: { // 画像中のテキスト表示エリアの左上・右下座標(左上基準)
        start: {
          x: 0.26,
          y: 0.1
        },
        end: {
          x: 0.72,
          y: 0.34
        }
      },
      cotext: null,
      text: "",
    }
  },
  mounted() {
    // init canvas
    const canvas = this.$refs.canvas;
    if (!canvas || !canvas.getContext) return false;
    
    // context
    this.context = canvas.getContext("2d");
    // align center(horizontal/vertical)
    this.context.textAlign = "center";
    this.context.textBaseline = "middle";
    
    // initial draw
    this.drawImage("");
    
  },
  methods: {
    drawText(text, startX, startY, endX, endY) {
      // set context setting
      this.context.font = `${this.font.size}px '${this.font.family}'`;
      const heightAdjustment = this.size.height * 0.01;
      
      let lines = [text];
      if (text.includes("\n")) lines = text.split("\n");
      const lineCount = lines.length;
      lines.forEach((line, idx) => {
        this.context.fillText(
          line,
          startX + (endX - startX) / 2,
          startY +
            (endY - startY) / 2 +
            idx * (this.font.size + this.font.lineHeightOffset) -
            ((this.font.size + this.font.lineHeightOffset) * (lineCount - 1)) /
              2 -
            heightAdjustment
        );
      });
    },
    drawImage(text) {
      console.log(this.size);
      // clear canvas
      this.context.clearRect(0, 0, this.size.width, this.size.height);
      
      // image obj
      const img = new Image();
      img.src = this.imgsrc;
      img.onload = () => {
        this.context.drawImage(img, 0, 0, this.size.width, this.size.height);
        this.drawText(
          text,
          this.textareaRate.start.x * this.size.width,
          this.textareaRate.start.y * this.size.height,
          this.textareaRate.end.x * this.size.width,
          this.textareaRate.end.y * this.size.height
        );
      };
    },
    changeText(text) {
      this.drawImage(this.text);
    },
    changeFontSize() {
      this.drawImage(this.text);
    }
  }
})
              
            
!
999px

Console